medo medo - 1 year ago 56
Android Question

android Listview repeat data while scroll

i have

listview
with
BaseAdapter
this is my adapter code

public class MessageAdapter1 extends BaseAdapter {

Context context;
private int auth;
private List<MessageList> mMessages;
private LayoutInflater mInflater;




public MessageAdapter1(Context context, List<MessageList> messages) {
this.mMessages = messages;
this.context = context;

this.session = new SessionManager(context);
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
auth = session.getUserId();

}



@Override
public int getItemViewType(int position) {
int type = -1;
String m = mMessages.get(position).getType();
int user_idx = mMessages.get(position).getUser_id();


if(auth != user_idx && m.equals("message")) type = 1;
else if(auth == user_idx && m.equals("message")) type = 0;
else if(auth != user_idx && m.equals("image")) type = 3;
else if(auth == user_idx && m.equals("image")) type = 4;
//other else if and types .....
return type;
}

@Override
public int getViewTypeCount() {
return 19;
}

@Override
public int getCount() {
return mMessages.size();
}

@Override
public Object getItem(int i) {
return mMessages.get(i);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);

if (convertView == null) {
holder = new ViewHolder();
switch (type) {



case MessageList.TYPE_MESSAGE_RIGHT:
convertView = mInflater.inflate(R.layout.right_message, null);
holder.groupMessage = (LinearLayout)convertView.findViewById(R.id.messages);

break;
case MessageList.TYPE_MESSAGE_LEFT:
convertView = mInflater.inflate(R.layout.left_message, null);
holder.groupMessage = (LinearLayout)convertView.findViewById(R.id.messages);
break;
case MessageList.TYPE_ACTION:
convertView = mInflater.inflate(R.layout.typing, null);
holder.Indicator = (AVLoadingIndicatorView)convertView.findViewById(R.id.indicator);

break;
//other case break for other types ..




}

convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}

setMessage(holder,position);


return convertView;
}

private void setMessage(ViewHolder holder, int position){
MessageList m = mMessages.get(position);

//holder.groupMessage.add ... data





}


public static class ViewHolder {
private LinearLayout groupMessage;
private AVLoadingIndicatorView Indicator;
//more here other
}
}


I search in google and in stackoverflow all solution talk about
Override


`getViewTypeCount` and `getItemViewType` and other solution not helped me


i see my code its correct but why its repeat or reorder item when scroll on
ListView

Answer Source

Because your item views appear to change based on the type of items in the list, you cannot cache your viewholder.

Try removing your if (convertView == null) { and accompanying else { statement.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download