1

I am trying to update the listview with baseadapter during the presence notification in rosterlistener.

But the updated data not reflecting in listview. please help me.

below is my customadapter class.

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..

kavuru
  • 379
  • 3
  • 4
  • 14
  • Do you see updated values in logcat when you log contact_list elements in `presenceChanged()` method? Also do you see `"About to notify datasetchanged"`? – Misagh Emamverdi Sep 27 '14 at 13:47
  • For notifyDataSetChanged() to work, you have to use add/remove method on your Contact_List array. See the answer in http://stackoverflow.com/questions/3669325/notifydatasetchanged-example – joao2fast4u Sep 27 '14 at 13:48
  • @Misagh Yes, I could see that log. – kavuru Sep 27 '14 at 14:52
  • So you can create a setter method for Contact_list in your adapter and before you call `notifyDataSetChanged()` you should call `contactAdapter.setContactList(Contact_List)` – Misagh Emamverdi Sep 27 '14 at 17:15
  • i changed like that..but no luck..just now i observed one difference.. i could see only before log of contactAdapter.notifyDatasetChanged() and no log is coming for after stmt.. it looks emulator getting stuck at that method. bcoz long press is working only at start of activity and not working after so changed contact_list. any help plz.. i spent whole day today..:( – kavuru Sep 27 '14 at 17:36
  • I resolved it by running notifydatasetchanged in runonui thread..good learning experience..!! – kavuru Sep 27 '14 at 18:06

1 Answers1

0

Does your method RosterOperations.createContactListByRosterEntries() create new ArrayList<Contact> instance ? If yes, you should refresh data in adapter first:

public void setNewData(ArrayList<Contact> newData) {
    Contact_List.clear();
    Contact_List.addAll(newData);
    this.notifyDataSetChanged();
}
skywall
  • 3,956
  • 1
  • 34
  • 52
  • it wont create new instance .. it's parameter list is passed with new ArrayList instance and inside objects were added. – kavuru Sep 27 '14 at 14:51
  • this is also not working.. listview is getting stuck after contact_list got updated. – kavuru Sep 27 '14 at 17:37