Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

notifydataSetChanged working but only showing 1 result in listview Android

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...