简体   繁体   中英

Android-Listview items change when scrolling

as the title says my items keep switching rows, when I scroll through the ListView. I already read here about the same issue but I cannot figure out what I've done wrong.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    View row = convertView;
    RecordHolder holder = null;
    Object o = getItem(position);

    if (row == null) {
        LayoutInflater inflater = activity.getLayoutInflater();
        row = inflater.inflate(R.layout.chatmessageitem,null);
        holder = new RecordHolder();
        holder.txtMessage = (TextView) row.findViewById(R.id.txttextmessage);
        holder.messageBubble = (ImageView) row.findViewById(R.id.chatbubble);

        if(o instanceof MessageObject){
            MessageObject m = (MessageObject) o;
            if(m.getEmpfänger() != ((GlobalClass) context).myChatID){
                holder.ID = m.getEmpfänger();
            }else{
                holder.ID =((GlobalClass) context).myChatID;
            }

            holder.message =(m.getMessage());
        }
        row.setTag(holder);
    }else{
        holder = (RecordHolder)row.getTag();
    }

    if(o instanceof MessageObject){
        MessageObject msg = (MessageObject)o;
        holder.txtMessage.setText(msg.getMessage());

        if(holder.ID != ((GlobalClass) context).myChatID){
            holder.messageBubble.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.bubbleone));
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            holder.txtMessage.setText(holder.message);
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            holder.messageBubble.setLayoutParams(lp);
        }else{
            holder.messageBubble.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.bubbletwo));
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            holder.txtMessage.setText(holder.message);
            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            holder.messageBubble.setLayoutParams(lp);
        }
    }
    return row;
}

static class RecordHolder {
    String message;
    int ID;
    TextView txtMessage;
    ImageView messageBubble;
}

thx for any answeres in advance. Regards.

You're setting holder.ID based on the current MessageObject when the view is created, rather than for each MessageObject as the view is recycled. The content is then set based on that initial (likely erroneous) ID but using the current MessageObject .

Move this logic:

    if(o instanceof MessageObject){
        MessageObject m = (MessageObject) o;
        if(m.getEmpfänger() != ((GlobalClass) context).myChatID){
            holder.ID = m.getEmpfänger();
        }else{
            holder.ID =((GlobalClass) context).myChatID;
        }

        holder.message =(m.getMessage());
    }

to after the if (row == null) {...} else {...} block. (Or incorporate it into the following if(o instanceof MessageObject){... block for cleaner code.

Try adding these methods to your adapter class:

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

  @Override
  public int getItemViewType(int position) {
   return position;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM