2014-09-27 62 views
1

我想在rosterlistener中的狀態通知期間用baseadapter更新listview。NotifyDataSetChanged不更新列表視圖

但更新的數據沒有反映在listview中。請幫幫我。

下面是我的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; 

    } 

} 

監聽器:

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

     } 

活動代碼:

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

所以我是缺少在這裏才能正常工作..

+0

當您在'presenceChanged()'方法中記錄contact_list元素時,是否在logcat中看到更新的值?你也看到''關於通知datasetchanged「'? – 2014-09-27 13:47:02

+0

要使notifyDataSetChanged()正常工作,必須在Contact_List數組上使用add/remove方法。請參閱http://stackoverflow.com/questions/3669325/notifydatasetchanged-example中的答案 – joao2fast4u 2014-09-27 13:48:21

+0

@Misagh是的,我可以看到該日誌。 – kavuru 2014-09-27 14:52:34

回答

0

是否你的方法RosterOperations.createContactListByRosterEntries()創建新ArrayList<Contact>實例?如果是的話,您應該先刷新適配器中的數據:

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

它不會創建新的實例..它的參數列表傳遞與新的ArrayList 實例和裏面的對象被添加了。 – kavuru 2014-09-27 14:51:29

+0

這也不起作用。在contact_list更新後,listview被卡住了。 – kavuru 2014-09-27 17:37:34