Итак, я работаю над довольно упрощенным приложением для SMS, чтобы научить себя Android, и, похоже, я столкнулся с той же проблемой, описанной в этой теме: ListView in ArrayAdapter order get's mixed up when scrollingAndroid ListFragment скремблирует порядок элементов, которые изначально не отображаются
К сожалению, ответ, размещенный там, похоже, не работает для меня. Я смотрел на это уже давно, и я в тупике. Обратите внимание: поскольку это приложение для SMS, я использую два разных макета, в зависимости от того, кто отправил сообщение.
Кроме того, путем отладки я обнаружил, что сообщения, которые я передаю в моем ArrayList, находятся в правильном порядке, так что это не проблема. Любая помощь будет потрясающей.
public class MessageAdapter extends ArrayAdapter {
public MessageAdapter(Context context, ArrayList<CryptoMessage> messages){
super(context, 0, messages);
}
/**
* {@inheritDoc}
*
* @param position
* @param convertView
* @param parent
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CryptoMessage current = (CryptoMessage) getItem(position);
if(convertView == null){
if(current.isSent()){
//This is message that the user sent. Inflate the view for a sent message.
convertView = LayoutInflater.from(getContext()).inflate(R.layout.sent_message, parent, false);
}
else{
//Otherwise, this is a message that the user received. Inflate the view for a received message.
convertView = LayoutInflater.from(getContext()).inflate(R.layout.recieved_message, parent, false);
}
}
if(current.isSent()){
//The current message is a sent message. Get the resources from the sent view.
TextView body = (TextView) convertView.findViewById(R.id.sentMessageText);
TextView date = (TextView) convertView.findViewById(R.id.sentMessageDate);
//Populate the data.
if(body != null) {
body.setText(current.getMessage());
}
if(date != null) {
date.setText(current.getDate());
}
}
else{
TextView body = (TextView) convertView.findViewById(R.id.recievedMessageText);
TextView date = (TextView) convertView.findViewById(R.id.recievedTimestampText);
if(body != null) {
body.setText(current.getMessage());
}
if(date != null) {
date.setText(current.getDate());
}
}
return convertView;
}
}
Отлично! Это прекрасно работает! Я думаю, что учебник, который я использовал, был не так уж хорош. – chucklay