当回$ P $从另一个活动的Andr​​oid pssed如何更新列表视图? [英] How to update listview when back pressed from another activity android?

查看:292
本文介绍了当回$ P $从另一个活动的Andr​​oid pssed如何更新列表视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨如何更新的ListView 回来时$ P $在另一个活动pssed。我使用的数据库中,我在有一个活动中删除特定的行,仿佛又回到了$ P $独自pssed,它没有更新。当我切换到关闭了一些其他的活动或应用程序,然后打开的ListView 得到更新它的正常工作。

Hi how to update listView when back pressed in another activity. I am using database, where i am deleting particular row in one Activity, if back pressed alone, it's not updating. When i switch over to some other activity or app closed and open it's working fine where listView gets updated.

下面是code ..

Here is code..

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 活动,我删除一条记录,如果我来到这个画面,即$ P $通过回preSS pvious活动,它仍然是同一个列表,它不是更新。但是,当我关闭应用程序和开放的,它的正常工作。

In ExpertChatView activity, I am deleting a record and if i come to this screen, ie, previous activity by back press, it still remains the same list and it's not updating. But when i close the app and open, it's working fine.

在expertChatView活动,我删除用户名的纪录。

In expertChatView activity, i am deleting username record.

AAEDatabaseHelper.deleteUsername(<username>);

我尝试用 notifyDatasetChanged 方法 onResume()但还是同样的问题。

I tried by using notifyDatasetChanged method in onResume() but still same issue.

修改

EDIT

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

//此处查看holder类...

// view holder class here...

推荐答案

在下面的方法适配器首先写

First write below method in Adapter

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

在简历从数据库中读取数据,并调用updateList(名单mChatDetails)方法,然后再调用notifyDatasetChanged()

In resume fetch the data from database and call updateList(List mChatDetails) method then call notifyDatasetChanged()

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

这篇关于当回$ P $从另一个活动的Andr​​oid pssed如何更新列表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆