如何在从另一个活动返回时更新Android中的ListView?

6

嗨,如何在返回另一个活动时更新listView。我正在使用数据库,在一个Activity中删除特定行,如果仅按下后退键,则不会更新。当我切换到其他活动或关闭并打开应用程序时,它可以正常工作,其中listView得到更新。

这是代码:

public class FragmentLiveChats extends Fragment {
    private AdapterLiveChat adapterChatHistory;
    private List<LiveChatDetails> customerDetail;
    private ListView mListView;
       private List<LiveChatDetails> list;
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {  
list =AAEDatabaseHelper.getLiveChatDetails(Constants.TABLE_LIVE_CHAT);   
    mListView = (ListView)   rootView.findViewById(R.id.list_chat_history);
      parseResponse();
}

private void parseResponse(){
 for(int i=0;i<list.size();i++){
        LiveChatDetails chatDetailsList = new LiveChatDetails();
        chatDetailsList.setId(list.get(i).getId());
       chatDetailsList.setFromUsername(list.get(i).getFromUsername());
        chatDetailsList.setChatTime(list.get(i).getChatTime());
        chatDetailsList.setLatMsg(list.get(i).getLatMsg());
        customerDetail.add(chatDetailsList);
    }
    setValuesInAdapter();
 }
 @Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    adapterChatHistory.updateList(customerDetail);
    adapterChatHistory.notifyDataSetChanged();
}

private void setValuesInAdapter() {
    if (adapterChatHistory == null) {
        adapterChatHistory = new AdapterLiveChat(context);
    }
    adapterChatHistory.setExpertData(customerDetail);
    if (customerDetail != null && !customerDetail.isEmpty()) {
        mListView.setVisibility(View.VISIBLE);
        viewNoData.setVisibility(View.GONE);
        mListView.setAdapter(adapterChatHistory);
    } else {
        mListView.setVisibility(View.GONE);
        viewNoData.setVisibility(View.VISIBLE);
    }
    adapterChatHistory.notifyDataSetChanged();
    mListView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            LiveChatDetails chatDetails = (LiveChatDetails) parent.getAdapter()
                                                .getItem(position);
            Log.e("chatDetails", "chat details"+chatDetails.getId());
            Intent intent = new Intent(context,ActivityExpertChatView.class);
            intent.putExtra(Constants.CHAT_USER_NAME, chatDetails.getId());
            startActivity(intent);
        }
    });
}

在适配器中:

public void setExpertData(List<LiveChatDetails> mTempData) {
    this.mTempData = mTempData;
}

ExpertChatView活动中,我正在删除一条记录,如果我通过返回键回到上一个活动,它仍然保留着相同的列表,并且没有更新。但是当我关闭应用程序并重新打开时,它可以正常工作。
在expertChatView活动中,我正在删除用户名记录。
AAEDatabaseHelper.deleteUsername(<username>);

我尝试在onResume()方法中使用notifyDatasetChanged方法,但仍然存在相同的问题。

编辑

 Adapter class 

public class AdapterLiveChat extends BaseAdapter {
private Context context;
private List<LiveChatDetails> mTempData;
private LayoutInflater mInflater;
private LiveChatDetails liveChatDetails;

private View adView;
private ViewHolder holder;

private String userImageUrl;


public AdapterLiveChat(Context context) {
    this.context = context;
    imgLoader = Picasso.with(context);
    mCalendar = Calendar.getInstance();
}

public void setExpertData(List<LiveChatDetails> mTempData) {
    this.mTempData = mTempData;
}

public void updateList(List<LiveChatDetails> mChatDetails) {
    this.mTempData = mChatDetails;
}

@Override
public int getCount() {
    return mTempData.size();
}

@Override
public LiveChatDetails getItem(int position) {
    return mTempData.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}
 @Override
 public View getView(int position, View convertView, ViewGroup parent)                      {
    mInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    liveChatDetails = getItem(position);
    adView = convertView;
    if (adView == null) {
        holder = new ViewHolder();
        adView = mInflater.inflate(R.layout.row_live_chats, null);
        holder.txtName = (CustomTextView) adView
                .findViewById(R.id.txt_name);
     adView.setTag(holder);
    } else
        holder = (ViewHolder) adView.getTag();
    holder.txtName.setText(liveChatDetails.getFromUsername());
   return adView;
   }

// 这里是视图持有者类...


显然,notifyDatasetChanged并不是什么魔法棒……如果您不修改底层数据,它是无济于事的…… - Selvin
在调用NotifyDataSetChanged()之前,您需要更新数据列表。 - Chol
将您更新的数据添加到ListView的适配器中,然后在返回时通知它。 - Ajay Pandya
是的,这只涉及本地数据。在另一个活动中,我正在删除记录。在之前的片段中,我也需要删除吗?@HradeshKumar - Star
@star 你之前的片段数据和以前一样,所以你需要从数据库重新加载它。 - Hradesh Kumar
显示剩余4条评论
3个回答

4

首先在适配器中编写以下方法

    public void updateList(List<Messages> mChatDetails) {
    this.mChatDetails.clear();
    this.mChatDetails.addAll(mChatDetails)
}

在简历中,从数据库获取数据并调用updateList(List mChatDetails)方法,然后调用notifyDatasetChanged()。

    @Override
public void onResume() {
    // fetch updated data
    adapter.updateList(mChatDetails);
    adapter.notifyDataSetChanged();
}

你能否在onResume中发布你的代码?这种方法绝对有效。我很久以前就一直在使用这种方法。 - Prashanth Debbadwar
请检查,您在onResume中没有再次获取数据。为了最佳实践,请维护一个标志,以便您可以避免在onResume中反复获取数据。 - Prashanth Debbadwar
看一下我编辑过的代码。在哪里使用parseResponse?在onResume中吗?因为当我使用它时,会重复两个列表值。 - Star
1
尝试使用mChatDetails.addAll(mChatDetails)而不是mChatDetails = mChatDetails。如果您想在添加所有新项目之前删除项目,可以调用this.mChatDetails.clear()。 - Aksiom
是的,完成了。public void onResume() { super.onResume(); customerDetail.clear(); parseResponse(); adapterChatHistory.updateList(customerDetail); adapterChatHistory.notifyDataSetChanged(); } 但问题仍然存在。 - Star
显示剩余9条评论

0
customerDetail列表中删除该项,然后调用notifydatasetchanged()

请问您能否指出我在代码中哪里犯了错误? - Star

0
在Activity1中调用finish()函数。
Intent intent = new Intent(getApplicationContext(), Activity2.class);
startActivity(intent);
finish();

在Activity2中重写backPressed方法。
public void onBackPressed(){
    Intent i = new Intent(this,AdminActivity.class);
    startActivity(i);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接