Мой пользовательский адаптер содержит метод getView, а позиция getView не увеличивается правильно. Побочные эффекты - это то, что все значения представлены в обеих макетах (row_chat_me row_chat_others). IsMsgFromMe правильно определяет, кто автор сообщения (message.GetFrom()). chatMsgList - это объект ArrayList, содержащий объекты Message.Android - Пользовательский адаптер, позиция GetView не увеличивается правильно
Спасибо!
public class ChatAdapter extends ArrayAdapter<Message> {
public ChatAdapter(Context context, ArrayList<Message> messages) {
super(context, 0, messages);
}
public boolean IsMsgFromMe(Message message) {
boolean isSenderMe = ChatFragment.username.equals(message.GetFrom());
return isSenderMe;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// Get the data item for this position
Message message = ChatFragment.chatMsgList.get(position);
if (convertView == null) {
holder = new ViewHolder();
//Check who has sent the message me or someone else...
if (IsMsgFromMe(message)) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_chat_me, parent, false);
holder.chatFrom = (TextView) convertView.findViewById(R.id.txt_message_me_from);
holder.chatMessage = (TextView) convertView.findViewById(R.id.txt_message_me_message);
holder.chatTime = (TextView) convertView.findViewById(R.id.txt_message_me_time);
} else {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_chat_others, parent, false);
holder.chatFrom = (TextView) convertView.findViewById(R.id.txt_message_others_from);
holder.chatMessage = (TextView) convertView.findViewById(R.id.txt_message_others_message);
holder.chatTime = (TextView) convertView.findViewById(R.id.txt_message_others_time);
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Populate the data into the template view using the data object
holder.chatFrom.setText("From: " + message.GetFrom());
holder.chatMessage.setText(message.GetMsg());
holder.chatTime.setText("Date: " + message.GetTime());
// Return the completed view to render on screen
return convertView;
}
public static class ViewHolder {
public TextView chatFrom;
public TextView chatMessage;
public TextView chatTime;
}
}
Привет, я реализовал это решение, но, к сожалению, проблема остается прежней. Вот как я его реализовал http://pastebin.com/tSqtz4aJ – user2407226
@ user2407226 Я думаю, вы должны удалить notifyDataSetChanged(); в getView (...). Если он все еще не работает, пожалуйста, покажите мне Logcat или более подробную информацию об этой проблеме (я не совсем понимаю, что «позиция не увеличивается правильно»). – VAdaihiep
Теперь я нашел решение своего вопроса, но, к сожалению, у меня есть некоторые проблемы при прокрутке списка, и мое решение для этой проблемы работает, но это ужасное решение и уничтожает цель из шаблона Holder. Я напишу новый вопрос о stackoverflow, чтобы избежать путаницы в будущем, «http://stackoverflow.com/questions/28356606/android-view-holder-does-not-update-listview-correctly» Решение заключалось в том, чтобы удалить второй список и создать левую и правую стороны в строке. См. Код "http://pastebin.com/6WSjQAbs", "http://pastebin.com/xMFCm62s", "http://pastebin.com/rCbeBS6E". – user2407226