【问题标题】:How to update listview when back pressed from another activity android?从另一个活动android返回时如何更新listview?
【发布时间】:2016-02-16 11:07:04
【问题描述】:

您好,在另一个活动中按下后如何更新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 不是什么魔杖...如果您不修改底层数据,它将无济于事...
  • 您还需要在调用 NotifyDataSetChanged() 之前更新数据列表
  • 将更新后的数据添加到 listview 的适配器,然后在后台通知它
  • 在 onResume 事件中,您必须为“customerDetail”重新生成数据,然后您必须调用 notifyDatasetChanged。
  • 在 OnRestart 或 Activity 的 onResume 方法上需要调用 notifyDataSetChanged();。也不要忘记从您的列表和数据库中删除数据。在您的情况下,我认为您是从数据库中删除数据而不是从列表中删除数据。检查一次。

标签: android listview adapter android-adapter


【解决方案1】:

先在Adapter中写下方法

    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 中吗?它绝对有效。很久以前我就在使用这种方法。
  • 您没有在 onresume 中再次获取数据,请检查。为了最佳实践,维护一个标志,这样您就可以避免在 onresume 中一次又一次地获取数据
  • 是什么意思?我需要再一次调用那个 for 循环吗?
  • 显然你也有。您需要提供更新的 arraylist 以便 listview 可以显示新的更改。编写一个获取数据的方法,以便您可以重用代码。
  • 尝试 mChatDetails.addAll(mChatDetails) 而不是 mChatDetails = mChatDetails。如果要在添加所有新项目之前删除项目,可以调用 this.mChatDetails.clear()。
【解决方案2】:

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

【讨论】:

  • 请你显示我在哪里做错代码
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 2023-04-02
    • 2015-06-12
    • 1970-01-01
    • 2016-07-24
    • 2018-03-17
    相关资源
    最近更新 更多