如何在从另一个活动返回后更新选项卡活动中的列表视图

时间:2014-05-22 12:48:15

标签: android

我的代码是:

这是我的重置列表视图适配器的代码,但当我来自另一个选项卡时,然后在列表视图中添加重复的数据。

String sortedListByName=parent.getItemAtPosition(position).toString().toUpperCase();

tempContactList.clear();

for (Contact cn : contact) {

    String name=cn.getName();
    String subname=name.substring(0, 1);

    if(subname.toString().toUpperCase().startsWith(sortedListByName))
    {   

        HashMap<String, String> sortedListData = new HashMap<String, String>();
        sortedListData.put(TAG_Photo, cn.get_uid());
        sortedListData.put(TAG_NAME, cn.getName());
        sortedListData.put(TAG_CompanyName, cn.getCompany());
        sortedListData.put(TAG_BizPhone, cn.getPhoneNumber());
        sortedListData.put(TAG_BizAddressCity, cn.get_city());
        sortedListData.put(TAG_Email, cn.getEmail());
        sortedListData.put(KEY_country, cn.getCountry());

        sortedListData.put(KEY_BizFax, cn.getBizFax());
        sortedListData.put(KEY_street, cn.getStreet());
        sortedListData.put(KEY_state, cn.getState());

        sortedListData.put(KEY_zip, cn.get_zip());
        sortedListData.put(KEY_BizPhone2, cn.getBizPhone2());
        sortedListData.put(KEY_BlackBerryPin, cn.getBlackBerryPin());
        sortedListData.put(KEY_Categories, cn.getCategories());
        sortedListData.put(KEY_ContactID, cn.getContactID().toString());
        sortedListData.put(KEY_ContactType, cn.getContactType());
        sortedListData.put(KEY_Department, cn.getDepartment());
        sortedListData.put(KEY_DirectPhone, cn.getDirectPhone());
        sortedListData.put(KEY_Email2, cn.getEmail2());
        sortedListData.put(KEY_Email3, cn.getEmail3());
        sortedListData.put(KEY_EmailDisplayName, cn.getEmailDisplayName());
        sortedListData.put(KEY_FullName, cn.getFullName());
        sortedListData.put(KEY_HomeAddressCity, cn.getHomeAddressCity());
        sortedListData.put(KEY_HomeAddressCountry, cn.getHomeAddressCountry());
        sortedListData.put(KEY_HomeAddressState, cn.getHomeAddressState());
        sortedListData.put(KEY_HomeAddressStreet, cn.getHomeAddressStreet());
        sortedListData.put(KEY_HomeAddressZip, cn.getHomeAddressZip());
        sortedListData.put(KEY_HomeFax, cn.getHomeFax());
        sortedListData.put(KEY_HomePhone, cn.getHomePhone());
        sortedListData.put(KEY_HomePhone2, cn.getHomePhone2());
        sortedListData.put(KEY_IMAddress, cn.getIMAddress());
        sortedListData.put(KEY_JobCategory, cn.getJobCategory());
        sortedListData.put(KEY_JobTitle, cn.getJobTitle());
        sortedListData.put(KEY_LastName, cn.getLastName());
        sortedListData.put(KEY_MiddleName, cn.getMiddleName());
        sortedListData.put(KEY_Mobile, cn.getMobile());
        sortedListData.put(KEY_NameTitle, cn.getNameTitle());
        sortedListData.put(KEY_Note, cn.getNote());
        sortedListData.put(KEY_OfficeLocation, cn.getOfficeLocation());
        sortedListData.put(KEY_Pager, cn.getPager());
        sortedListData.put(KEY_SuffixName, cn.getSuffixName());
        sortedListData.put(KEY_WebPage, cn.getWebPage());


        tempContactList.add(sortedListData);


    }


}
if(position==0)
{
    //modified by vikas
    tempContactList.addAll(tempContactListSearch);

}   

adapter = new ListViewAdapter(getParent(), tempContactList);
listview.setAdapter(adapter);

//请在添加新内容之前帮助您清除所有数据。

1 个答案:

答案 0 :(得分:0)

创建一个bool变量ifFirsttime最初全局保持为true,然后当适配器instanse生成

 if(ifFirsttime)
 {

  adapter = new ListViewAdapter(getParent(), tempContactList); 

  listview.setAdapter(adapter);
  ifFirsttime=false;


 }
 else

 adapter.notifyDataSetChanged();

在onResume方法中编写代码 我认为它会对你有所帮助