-2

Hi i have an issue where only 1 result is being shown when i call a notifydataSetChanged function. I have read in this forum that the Array list needs to be cleared, then the data needs to be added for the notifydataSetChanged to work properly. I have followed that, but the problem is when the new data arrives it clears all the other data and only shows the latest item from the list. I know its a clear() issue, but i cant work out how to fix it. Any suggestions would be gratefully appreciated.

My Adapter :

public MessageAdapter(Activity context, ArrayList<ArrayList<ReadMessage>> list) {
    mContext = context;
    mList = list;
    mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

@Override
public Object getItem(int pos) {
    return mList.get(pos);
}

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


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    CompleteListViewHolder viewHolder;
    if (convertView == null) {
        LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = li.inflate(R.layout.list_row, null);
        viewHolder = new CompleteListViewHolder(v);
        v.setTag(viewHolder);
    } else {
        viewHolder = (CompleteListViewHolder) v.getTag();
    }
    viewHolder.messageText.setText(mList.get(position).get(position).getMessage());
    viewHolder.fromText.setText(mList.get(position).get(position).getSender());
    return v;
}

}

class CompleteListViewHolder {
    public TextView messageText;
    public TextView fromText;

public CompleteListViewHolder(View base) {
    messageText = (TextView) base.findViewById(R.id.message);
    fromText = (TextView) base.findViewById(R.id.user);
}

The following method is where i am adding items to the list :

  private void addItemsToList() {
       sentMessages.clear();
         sentMessages.add(localstoragehandler.getUserComments(MessageService.USERNAME, friendUsername()));
      messageAdapter.notifyDataSetChanged();
}

The addItemsToList() is called when new data arrives from the web service.

Thanks

6
  • because it is not the same references .... adapter using (simple version) copy of it
    – Selvin
    Jun 9, 2015 at 13:39
  • Your notifyDataSetChanges is very strange, you need just to change data in the list if it needs to and call it, no refreshAdapter is needed
    – Nik Myers
    Jun 9, 2015 at 13:39
  • I only added refreshAdapter as i read on a post earlier that it may fix it
    – n4zg
    Jun 9, 2015 at 13:42
  • I have updated the code removing the refreshAdapter function.
    – n4zg
    Jun 9, 2015 at 13:46
  • Use list.invalidateview() instead notifydatasetchanged
    – Santiago
    Jun 9, 2015 at 13:47

1 Answer 1

0

What's probably happening is that your list sentMessages is what you used to initialize your MessageAdapter, correct? By calling mList.clear() since it points to the same object as sentMessages, you are clearing everything from both lists (which is really just the exact same list). What I would do is change your MessageAdapter constructor to the following:

public MessageAdapter(Activity context, ArrayList<ArrayList<ReadMessage>> list) {
    mContext = context;
    mList = new ArrayList<ArrayList<ReadMessage>>();
    mList.addAll(list);
    mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
4
  • Thank for the answer, i changed the consructor, but now im not getting no items in the array list. any sugestions please.
    – n4zg
    Jun 9, 2015 at 13:51
  • @n4zg Yeah, I took a second look and realized that might happen. Can I ask why you need to clear the ArrayList? I know you said you've read it on this forum, but I'm really not sure you need to. What happens if you keep your original code, but get rid of the clear() call and the addAll() call? So you just call sentMessages.add(...) and then notifyDataSetChanged()? Jun 9, 2015 at 13:57
  • il give it a go now , previously i have tried something similar with my constructor and it didnt update the arraylist, il try with the your constructor now
    – n4zg
    Jun 9, 2015 at 13:59
  • removing clear() with new constructor, didnt work, so i reverted to my constructor, i get all the results in the listview but when new data arrives the listview is not updated until i refresh the whole fragement
    – n4zg
    Jun 9, 2015 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.