如何从另一个活动android回来时更新listview?

android listview adapter android-adapter

3397 观看

3回复

334 作者的声誉

嗨如何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>);

我尝试使用notifyDatasetChanged方法,onResume()但仍然是同样的问题。

编辑

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

//在这里查看持有人类......

作者: Star 的来源 发布者: 2016 年 2 月 16 日

回应 3


0

1974 作者的声誉

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

作者: Akshay Bhat 'AB' 发布者: 2016 年 2 月 16 日

4

911 作者的声誉

决定

首先在Adapter中写下以下方法

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

在resume中从​​数据库中获取数据并调用updateList(List mChatDetails)方法然后调用notifyDatasetChanged()

    @Override
public void onResume() {
    // fetch updated data
    adapter.updateList(mChatDetails);
    adapter.notifyDataSetChanged();
}
作者: Prashanth Debbadwar 发布者: 2016 年 2 月 16 日

0

1 作者的声誉

在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);
}
作者: subha 发布者: 2019 年 5 月 14 日
32x32