0

My listview keeps changing while scrolling. I'm trying to display the messages in "messages" variable. Everything is fine when i scroll first time. But when i scroll again, the textviews overlap each other. I'm not sure what the problem is.

public class DisplayMessageAdapter extends ArrayAdapter<Message> {

    Context context;
    int resource;
    ArrayList<Message> messages = null;

    public DisplayMessageAdapter(Context context, int resource, ArrayList<Message> messages) {
        super(context, resource, messages);
        this.context = context;
        this.resource = resource;
        this.messages = messages; //list of messages to display
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;
        final MessagesHolder holder;


        if(row == null)
        {
            //Log.i("row-null","row-null");
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(resource, parent, false);

            holder = new MessagesHolder();
            holder.sent  = (TextView) row.findViewById(R.id.sent_message);
            holder.received = (TextView) row.findViewById(R.id.received_message);
            row.setTag(holder);
        }

        else
        {
            //Log.i("holder-not-null","holder-not-null");
            holder = (MessagesHolder) row.getTag();
        }

        Message message = messages.get(position);


        if(message.sent != null)
        {
            holder.sent.setText(message.sent);
            holder.received.setBackground(null);
        }
        else
        {
            holder.received.setText(message.received);
            holder.sent.setBackground(null);
        }

        return row;
    }

    static class MessagesHolder
    {
        TextView sent;
        TextView received;

    }

}

Layout xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
      <TextView android:id="@+id/sent_message"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:textColor="#000000"
        android:background="@drawable/sent"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp"

        android:textSize="15sp"
         />

      <TextView
          android:id="@+id/received_message"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@drawable/rcvd"
          android:layout_marginRight="20dp"
          android:textColor="#000000"
          android:layout_marginTop="4dp"
          android:layout_marginBottom="4dp"
          android:textSize="15sp"
           />

</RelativeLayout>
tshepang
  • 12,111
  • 21
  • 91
  • 136
brainfreak
  • 137
  • 1
  • 13
  • You have this `holder.received.setText(message.received);` and this `holder.sent.setText(message.sent);` twice remove one of them – Raghunandan Dec 23 '13 at 05:17
  • @GrIsHu no need for that coz op extends ArrayAdapter and has `super(context, resource, messages);` – Raghunandan Dec 23 '13 at 05:18

3 Answers3

0

Try to change your conditions as below to set the values in your TextView. In your below code the all if conditions are same. That does not make any difference.

  if(message.sent != null && message.received != null)
    {
        Log.i("adapter","both");
        holder.sent.setText(message.sent);
        holder.received.setText(message.received);
    }
    else if(message.sent != null)
    {
        holder.sent.setText(message.sent);
        //holder.received.setBackground(null);
    }
    else if(message.received != null)
    {
        holder.received.setText(message.received);
        //holder.sent.setBackground(null);
    }

Change it as below:

  if(message.sent != null && message.received != null)
    {
        holder.sent.setText(message.sent);
        holder.received.setText(message.received);
    }
    else
    {
        holder.sent.setText("NA");
         holder.received.setText("NA");
    }
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • Actually, this is a dead code(was seeing if this was executing). I have changed it to the one i was originally using. A message will have either sent or received with data. Not both. – brainfreak Dec 23 '13 at 05:37
  • @user3128485 i think what you need is different row types. one of receive one for message. http://stackoverflow.com/questions/18868194/android-xml-layout-for-a-listview-with-different-items – Raghunandan Dec 23 '13 at 05:50
  • You mean a row for send and a row for received? – brainfreak Dec 23 '13 at 06:18
0

Temproary Solution :

The problem seems to be in the holder and it's use. It works now if i dont use the holder class for performance boost. Although i'm not sure how to use the holder properly.

brainfreak
  • 137
  • 1
  • 13
0

I had a similar issue. I had a progress bar in my rows. My problem is that I wasn't resetting the progress bar every time get view was called. You must reset ever asset.

Programmer9000
  • 1,959
  • 3
  • 17
  • 27