简体   繁体   English

NotifyDataSetChanged不更新列表视图

[英]NotifyDataSetChanged not updating the list view

I am trying to update the listview with baseadapter during the presence notification in rosterlistener. 我正在尝试在rosterlistener中的状态通知期间使用baseadapter更新listview。

But the updated data not reflecting in listview. 但是更新的数据没有反映在列表视图中。 please help me. 请帮我。

below is my customadapter class. 下面是我的customadapter类。

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ContactAdapter extends BaseAdapter{

    private Context mContext;
    private ArrayList<Contact> Contact_List;
    Contact contact;
    String Name,Number,Status;
    ImageView item_image;
    TextView tv_Name,tv_Number,tv_Status;

    public ContactAdapter(Context c, ArrayList<Contact> C_List){

        super();
        mContext = c;
        Contact_List = C_List;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return Contact_List.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int arg0, View view, ViewGroup arg2) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.item, null);
        contact = Contact_List.get(arg0);
        Name = contact.getName();
        Number = contact.getNumber();
        Status = contact.getStatus();

        tv_Name = (TextView) view.findViewById(R.id.Name);
        tv_Number = (TextView) view.findViewById(R.id.Number);
        tv_Status = (TextView) view.findViewById(R.id.status);

        tv_Name.setText(Name);
        tv_Number.setText(Number);
        tv_Status.setText(Status);

        return view;

    }

}

Listener : 听众

RosterListener update_contacts = new RosterListener() {

        @Override
        public void presenceChanged(Presence presence) {

            String Name="",Status="";
            Log.d(Constants.TAG, "Presence value = "+presence);
            Log.d(Constants.TAG, "Presence getfrom value = "+presence.getFrom());
            Log.d(Constants.TAG, "Presence getstatus value = "+presence.getStatus());
            Log.d(Constants.TAG, "Presence getto value = "+presence.getTo());
            Name = presence.getFrom().substring(0, presence.getFrom().indexOf('@'));
            Status = presence.getStatus();
            Log.d(Constants.TAG, "Name value = "+Name);
            Log.d(Constants.TAG, "About to update contact list");
            RosterOperations.updateContactListByPresenceNotification(Name,Status,Contact_List);
            for(Contact c : Contact_List){
                Log.d(Constants.TAG, "name :"+c.getName());
                Log.d(Constants.TAG, "status :"+c.getStatus());
                Log.d(Constants.TAG, "number : "+c.getNumber());
            }
            Log.d(Constants.TAG, "About to notify datasetchanged");
            contactAdapter.notifyDataSetChanged();
            Log.d(Constants.TAG, "notify datasetchanged done");

        }

Activity code: 活动代码:

   Collection<RosterEntry> entries;
    ListView lv_roster;
    ArrayList<Contact> Contact_List;
    ContactAdapter contactAdapter;
    Context context;
Roster roster;
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_result);
            context =this;
            lv_roster = (ListView) findViewById(R.id.user_list);
            Contact_List = new ArrayList<Contact>();
            //Get the Roster from server
            Log.d(Constants.TAG, "Getting the roster from connection");
            roster = MainActivity.connection.getRoster();
            roster.addRosterListener(update_contacts);
            Log.d(Constants.TAG, "Getting the entries from roster");
            entries = roster.getEntries();
            Log.d(Constants.TAG, "Getting the entries from roster");
            RosterOperations.createContactListByRosterEntries(entries,Contact_List,roster);
            contactAdapter = new ContactAdapter(context, Contact_List);
            lv_roster.setAdapter(contactAdapter);
        }

so what I am missing here to work properly.. 所以我在这里无法正常工作..

Does your method RosterOperations.createContactListByRosterEntries() create new ArrayList<Contact> instance ? 您的方法RosterOperations.createContactListByRosterEntries()是否创建新的ArrayList<Contact>实例? If yes, you should refresh data in adapter first: 如果是,则应首先刷新适配器中的数据:

public void setNewData(ArrayList<Contact> newData) {
    Contact_List.clear();
    Contact_List.addAll(newData);
    this.notifyDataSetChanged();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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