Om Prakash Gupta
Om Prakash Gupta

Reputation: 111

How to update and notify the ListView after updating its data in android

I have two activities, ItemListActivity and ItemDetailsActivity, and using listview and custom Adapter to display data view. After clicking the item the ItemDetailsActivity is called and record is updated using API. Now I am coming back to ItemListActivity, here I am not fetching the data from API because I am having the updated data in my hand. Here I just want to update that particular records of listview without reloading the entire records. If my process is wrong then suggest me the proper way because I am beginner in Android Development.

Code :

protected void onStart() {
    super.onStart();
    String csThumbImageFld;
    try{
        final cGlobalData clsGlobal = (cGlobalData) getApplicationContext();
        csThumbImageFld = clsGlobal.getThumbImageFld();
        csThumbImageFld = csThumbImageFld == null ? "" : csThumbImageFld;
        if (!csThumbImageFld.isEmpty()) {
            s_UpdateItemView(csThumbImageFld);
        }
    }catch (Exception ex){
        cMsgHandler.s_ShowError(this,ex.getMessage(), eEnums.eErrorType.Error);
    }finally {
        csThumbImageFld = null;
    }
}

private void s_UpdateItemView(String csThumbImageFld){
        View gridView;
        ImageView imgSS;
        byte[] decodedString;
        Bitmap oImg;
        try{
            gridView = MyDL.getChildAt(m_inPosition);
            imgSS = (ImageView) gridView.findViewById(R.id.img_SS);
            if(!csThumbImageFld.isEmpty()){
                 decodedString = Base64.decode(csThumbImageFld, Base64.DEFAULT);
                 oImg = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                 imgSS.setImageBitmap(oImg);
                 m_Adapter.notifyDataSetChanged();
             }
         }catch (Exception ex){
             cMsgHandler.s_ShowError(this,ex.getMessage(), eEnums.eErrorType.Error);
         }
 }

Upvotes: 0

Views: 58

Answers (1)

Patrick R
Patrick R

Reputation: 6857

Save click position when you goes to detail activity and when you come back to list activity, replace updated data at that position in array list. See code below:

arraylist.set(clickPosition,newData);
adapter.notifyDataSetchanged();

Upvotes: 1

Related Questions