How to update listview when back pressed from another activity android?
Asked Answered
C

3

6

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.

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

In adapter:

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

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.

In expertChatView activity, i am deleting username record.

AAEDatabaseHelper.deleteUsername(<username>);

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

// view holder class here...

Convexoconcave answered 16/2, 2016 at 11:7 Comment(9)
obviously notifyDatasetChanged is not some magic wand ... if you do not modify underlaying data it will not help ...Echovirus
You need to update also the data list before calling NotifyDataSetChanged()Judiejudith
add your updated data to listview's adapter and than notify it on backpressCrossstitch
In onResume event you have to regenerate the data for "customerDetail" and then you have to call notifyDatasetChanged.Natatorium
on OnRestart or onResume method of Activity you need to call notifyDataSetChanged();. also not forget to delete data from your list as well as Database. in your case i think you are deleting data from DB not from list. Check it once.Giarla
yes this purely deals with local data. In another activity, i am deleting the record. In previous fragment too i need to delete? @HradeshKumarConvexoconcave
@star your previous fragment data is same as it was previous. so you need to reload it from DB.Giarla
will you please explain with code.Convexoconcave
solved. thank u so muchConvexoconcave
A
4

First write below method in Adapter

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

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();
}
Amygdaline answered 16/2, 2016 at 11:25 Comment(14)
can you please post your code in onresume ?? It definitely works. Since long back I am using this approach.Amygdaline
You didn't fetch data again in onresume please check. For best practise maintain a flag so that you can avoid fetching data again and again in onresumeAmygdaline
means? once again i need to call that for loop?Convexoconcave
Obviously you have too. you need to give updated arraylist so that listview can display new changes. Write a method to fetch data so that you can reuse the code.Amygdaline
see my edited code. where to use parseResponse. in onResume? because when i use that, it is duplicating with two list values.Convexoconcave
Try mChatDetails.addAll(mChatDetails) and not mChatDetails = mChatDetails. You can call this.mChatDetails.clear() if you want to remove the items before you add all the new items.Zygote
in onresume before calling the parseResponse just clear the customerDetail list i.e customerDetail.clear()Amygdaline
yes done.. public void onResume() { super.onResume(); customerDetail.clear(); parseResponse(); adapterChatHistory.updateList(customerDetail); adapterChatHistory.notifyDataSetChanged(); } but still same issue.Convexoconcave
i couldn't get this. Try mChatDetails.addAll(mChatDetails) and not mChatDetails = mChatDetails. You can call this.mChatDetails.clear() what mistake in this?Convexoconcave
I edited answer for " mChatDetails.addAll(mChatDetails) and not mChatDetails = mChatDetails. " ... Please check in onresume whether you got new data or not....Amygdaline
with addAll you are sure that the list has been changed, that will trigger the notifyDataSetChanged() when you call it. Prashanth Debbadwar updated the answer, so check it out. But be sure to delete the old values before adding new, otherwise you will get a double of everything.Zygote
yes you are right @Zygote i am getting values two times. however i cleared in onResume right ie customerDetail.clear();?Convexoconcave
yea, you have to call the clear before the addAll. this.mChatDetails.clear()Zygote
yes worked. greatest mistake i done is i used this in onCreate. list = AAEDatabaseHelper.getLiveChatDetails(Constants.TABLE_LIVE_CHAT); i used in the method parseResponse.Convexoconcave
D
0

Delete that item from the customerDetail list and then call notifydatasetchanged()

Deviation answered 16/2, 2016 at 11:23 Comment(1)
will you please show where i am doing mistake in codeConvexoconcave
A
0

call finish() in Activity1

Intent intent = new Intent(getApplicationContext(), Activity2.class);
startActivity(intent);
finish();

In Activity2 override backPressed

public void onBackPressed(){
    Intent i = new Intent(this,AdminActivity.class);
    startActivity(i);
}
Archambault answered 14/5, 2019 at 6:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.