1

I have a List View in one activity with one info icon in custom adapter. When user taps on that info button then the next activity will open and after marking attendance in next activity when user taps the update button then the second activity should finish and first activity listview should be updated.

What i successfully did: I have successfully mark the attendance and change the color of listview but i did that after closing the second activity and restarting the first activity. In this way the listview gets updated because of starting activity again.

What I am unable to do: I want that when user taps on update button then only finish() will call and user returns to previous first activity with listview updated. But when i do so then the listview not get updated. I have to go back and open the activity again then the listview gets updated otherwise not. I do not want that. I also tried to notify adapter in the onResume method of first activity so that when user returns from second activity then the first activity listview adapter will be updated because of onResume method but it isn't work. Please Help

My Listview Activity Code:

public class TeacherWebserviceMainList extends Activity {
int attentedncemarkedCount = 0;
TextView addteacher;
DatabaseHandler databasehandler;
DetailsTeacherwebservice details;
String emis;

ArrayList<DetailsTeacherwebservice> addas = new ArrayList<DetailsTeacherwebservice>();
CustomAdapterTeacherWebservice cusadapter;
ArrayList<DetailsTeacherwebservice> teacherList;
private ListView listcontent = null;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.teacherwebservicemainlist );
    addteacher = (TextView) findViewById(R.id.addteachermenu);

    databasehandler = new DatabaseHandler(TeacherWebserviceMainList.this);
    listcontent = (ListView) findViewById(R.id.teacher_list);
    teacherList = databasehandler.teacherwebserviceList(emis);
    Rsults();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
        if(resultCode == RESULT_OK) {
            String update = data.getStringExtra("update");
            if(update.equals("1"))
            {
                //cusadapter.
                CustomAdapterTeacherWebservice adapter = new CustomAdapterTeacherWebservice(this, addas);

                listcontent.setAdapter(adapter);
            }
        }
    }
}

private void Rsults() {
    addas.clear();
    //DatabaseHandler databaseHandler=new DatabaseHandler(this);
    //ArrayList<ArrayList<Object>> data =  databaseHandler.abcTeacherNew();

    for (int p = 0; p < teacherList.size(); p++) {
        details = new DetailsTeacherwebservice();
        //ArrayList<Object> baris = data.get(p);
        details.setId(teacherList.get(p).getId());
        details.setTeachername(teacherList.get(p).getTeachername());
        details.setTeachercnic(teacherList.get(p).getTeachercnic());
        details.setTeacherno(teacherList.get(p).getTeacherno());
        details.setTeachergender(teacherList.get(p).getTeachergender());
        details.setAttendance(teacherList.get(p).getAttendance());
        details.setTeacherattendancedetails(teacherList.get(p).getTeacherattendancedetails());
        details.setAttendancedatesince(teacherList.get(p).getAttendancedatesince());
        details.setAttendancetrasnferschool(teacherList.get(p).getAttendancetrasnferschool());
        addas.add(details);
    }
    cusadapter = new CustomAdapterTeacherWebservice(TeacherWebserviceMainList.this, addas);
    listcontent.setAdapter(cusadapter);
    listcontent.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {


        }
    });
}

My List Adapter Code

 public class CustomAdapterTeacherWebservice extends BaseAdapter {

private static ArrayList<DetailsTeacherwebservice> searchArrayList;
DatabaseHandler databaseHandler;
private Context context;
private LayoutInflater mInflater;

public CustomAdapterTeacherWebservice(Context context, ArrayList<DetailsTeacherwebservice> results) {
    searchArrayList = results;
    mInflater = LayoutInflater.from(context);
    databaseHandler = new DatabaseHandler(context);
}

@Override
public int getCount() {
    return searchArrayList.size();
}

@Override
public Object getItem(int p) {
    return searchArrayList.get(p);
}

@Override
public long getItemId(int p) {
    return p;
}

public int getViewTypeCount() {

    return 500;
}



@Override
public int getItemViewType(int position) {

    return position;
}

@Override
public View getView(final int p, View v, ViewGroup parent) {
    ViewHolder holder;
    context = parent.getContext();
    if (v == null) {
        v = mInflater
                .inflate(R.layout.teacherwebserviceadapter, null);
        holder = new ViewHolder();

        holder.name = (TextView) v.findViewById(R.id.teacher_name);
        holder.cnic = (TextView) v.findViewById(R.id.teacher_cnic);
        holder.no = (TextView) v.findViewById(R.id.teacher_phone);
        holder.gender = (TextView) v.findViewById(R.id.gender);
        holder.status = (TextView) v.findViewById(R.id.status);
        holder.info = (ImageView) v.findViewById(R.id.edit);
        holder.l1 = (LinearLayout) v.findViewById(R.id.main);


        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }

    holder.name.setText(searchArrayList.get(p).getTeachername());
    holder.cnic.setText(searchArrayList.get(p).getTeachercnic());
    holder.no.setText(searchArrayList.get(p).getTeacherno());
    holder.gender.setText(searchArrayList.get(p).getTeachergender());
    holder.status.setText(searchArrayList.get(p).getAttendance());
    if (searchArrayList.get(p).getAttendance().equals("Absent"))
    {
        holder.l1.setBackgroundColor(Color.parseColor("#DB674D"));
    }
    if (searchArrayList.get(p).getAttendance().equals("Present"))
    {
        holder.l1.setBackgroundColor(Color.parseColor("#7EB674"));
    }
    if (searchArrayList.get(p).getAttendance().equals("Transfer Out"))
    {
        holder.l1.setBackgroundColor(Color.parseColor("#FBE87C"));
    }
    if (searchArrayList.get(p).getAttendance().equals("Resigned"))
    {
        holder.l1.setBackgroundColor(Color.parseColor("#4FC3F7"));
    }
    holder.info.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent view_order_intent = new Intent(context, Teacherwebservicemainlistupdate.class);
            view_order_intent.putExtra("ID", searchArrayList.get(p).getId());
            view_order_intent.putExtra("tname", searchArrayList.get(p).getTeachername());
            view_order_intent.putExtra("tgender", searchArrayList.get(p).getTeachergender());
            view_order_intent.putExtra("tcnic", searchArrayList.get(p).getTeachercnic());
            view_order_intent.putExtra("tno", searchArrayList.get(p).getTeacherno());
            view_order_intent.putExtra("tatt", searchArrayList.get(p).getAttendance());
            view_order_intent.putExtra("tattdetails", searchArrayList.get(p).getTeacherattendancedetails());
            view_order_intent.putExtra("tattdatesince", searchArrayList.get(p).getAttendancedatesince());
            view_order_intent.putExtra("tatttrasnferout", searchArrayList.get(p).getAttendancetrasnferschool());
            //context.startActivity(view_order_intent);
            ((Activity)context).startActivityForResult(view_order_intent, 1);
        }
    });
    return v;
}

static class ViewHolder {
    TextView name, cnic, no, gender,status;
    ImageView info;
    LinearLayout l1;

}

here in adapter code when INFO button is clicked then another activity starts in which user can update the attendance.

This is Update activity code when button clicked:

  update.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        DetailsTeacherwebservice schoolinfo = new DetailsTeacherwebservice();
                schoolinfo.setTeachername(teachername.getText().toString());
                schoolinfo.setTeacherno(teacherno.getText().toString());
                schoolinfo.setTeachercnic(teachercnic.getText().toString());
                schoolinfo.setTeachergender(teachergender.getText().toString());
                schoolinfo.setAttendance(teachergroupstr);
                schoolinfo.setTeacherattendancedetails(absentgrpstr);
                schoolinfo.setAttendancedatesince(txtDate.getText().toString());
                schoolinfo.setAttendancetrasnferschool(transferOutSchool.getText().toString());
                databasehandler.updateteacherwebservice(schoolinfo, emis, identity);
                Toast.makeText(Teacherwebservicemainlistupdate.this, "Updated Successfully", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.putExtra("update", "1");
                setResult(RESULT_OK, intent);
                finish();

            }
        }
    });

I can start the listview activity again when update button is clicked but that changes the index of the list item clicked i.e. because activity starts again. How ever what i want is that if i clicked on 10th item then when next activity opens and user updates attendance then it returns back to previous activity on same index position so that the user do not have to scroll again to go back on 10th item

Arslan Ali
  • 333
  • 5
  • 18
  • OP, refer to this link on [how to use setResult() to send data to previous activity](https://stackoverflow.com/questions/920306/sending-data-back-to-the-main-activity-in-android). – Jay Jan 10 '18 at 07:35
  • Edit your question and add the code from the first activity that shows the adapter being setup including how the source data fro the ListView is obtained. Basically in `onResume` you need to rebuild the data and then notify the adapter that the data has changed e.g. `adapter.notifydataSetChanged()` or `adapter.swapCursor(cursor)`. – MikeT Jan 10 '18 at 07:38
  • Ok let me edit the post and also try your suggestion – Arslan Ali Jan 10 '18 at 07:42
  • @ArslanAli all depends upon the source data. – MikeT Jan 10 '18 at 07:43
  • startActivityForResult and notifyDataSetChanged() will do your job. If this isn't you are doing something wrong. Post your code if you need more detailed answer. – Gaurav Chauhan Jan 10 '18 at 10:16
  • @GauravChauhan i have tried this but it doesn't work i.e. listview not gets updated. However if i write any Toast inside that ADD CODE part then toast will shown which means next activity is sending something and this activity is also receiving that message but if i write notifydataSetChanged() in add code part then nothing happens until i get back to other activity and comes back again –  Jan 11 '18 at 11:52
  • by add code part I am refering to @SurajVaishnav answer below –  Jan 11 '18 at 11:53
  • @GauravChauhan i have tried this but it doesn't work i.e. listview not gets updated. However if i write any Toast inside that ADD CODE part then toast will shown which means next activity is sending something and this activity is also receiving that message but if i write notifydataSetChanged() in add code part then nothing happens until i get back to other activity and comes back again –  Jan 11 '18 at 11:54

5 Answers5

2

Add this code for list item click:

 Intent i = new Intent(context, YourAcitivityName.class);
     i.putExtra("position",p);
    startActivityForResult(i, 1);

Add this code in update button:

Intent intent = new Intent();
intent.putExtra("update", "1");
intent.putExtra("position",getIntent().getIntExtra("position",0));
setResult(RESULT_OK, intent);        
finish();

When you get back from activity to the activity where list view is implemented this method called:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
         if(resultCode == RESULT_OK) {
             String update = data.getStringExtra("update");
             if(update.equals("1"))
             {
               // add your code to update list view
                 teacherList = databasehandler.teacherwebserviceList(emis);
                 Rsults();
                  YourAdapterClassName adapter = new YourAdapterClassName(this, arrList);

                  listView.setAdapter(adapter);
               listView.smoothScrollToPosition(getIntent().getIntExtra("position",0));
              }
         }     
    }
} 
Suraj Vaishnav
  • 5,731
  • 3
  • 29
  • 40
  • should the method be overridden or not? – Arslan Ali Jan 10 '18 at 07:57
  • No it doesn't work. I have tried with notifydataSetChanged() on add code part but it doesnt updated and remains the same until i closed the activity and opens again – Arslan Ali Jan 10 '18 at 08:14
  • This should work, you may be doing something wrong please show me your code – Suraj Vaishnav Jan 10 '18 at 15:12
  • @SurajVaishnav Suraj i have tried this but it doesn't work i.e. listview not gets updated. However if i write any Toast inside that ADD CODE part then toast will shown which means next activity is sending something and this activity is also receiving that message but if i write notifydataSetChanged() in add code part then nothing happens until i get back to other activity and comes back again –  Jan 11 '18 at 11:52
  • ok, then try to initialize adapter again and set your adapter to listview like: i added in my answer – Suraj Vaishnav Jan 11 '18 at 12:19
  • It still doesn't work. Toast is showing but listview not updating. Can you please answer this on chat –  Jan 11 '18 at 12:32
  • can you post you whole code, for both activity and adapter? – Suraj Vaishnav Jan 11 '18 at 12:34
  • @SurajVaishnav I have edited the post with all the code Please check –  Jan 11 '18 at 12:44
  • @SurajVaishnav Edited the post please check – Arslan Ali Jan 11 '18 at 13:04
  • @SurajVaishnav I JUST Seen your answer, let me try this one please – Arslan Ali Jan 12 '18 at 10:06
  • @SurajVaishnav it works but issue remains the same because as the adapter is again sets so the list starts again with 0 index. For example if i clicked on 20th item and mark attendance then comes back then it should display the 20th item screen not the start of the list. Please suggest what to do in this case – Arslan Ali Jan 12 '18 at 10:13
1

The following are two examples, the first is an example where the source for the ListView is a List (and in this case using a Custom Adapter), the second is where the source is a Cursor (using SimpleCursorAdapter)

The trap many fall into is just updating the List using something like :-

    myList = method();
    myAdapter.notifyDataSetChanged();

At a guess, that's the trap you've fallen into.

When what should be done is to use the List's methods to alter it (e.g. clear, add, addAll, remove).

I believe this explains the reasoning - Is Java “pass-by-reference” or “pass-by-value”?

Example 1 - List as the source :-

/**
 * Build or rebuild the List of TaskData's, instantiate the
 * Custom Array Adapter if needed and also notify the adapter
 * if the List may have been changed.
 */
private void prepTasksCustomArrayList() {
    // Determine if the adpater needs to be notified of changes
    boolean notify_dataset_changed = (mCsr != null && mCustomArrayAdapter != null);

    // If the List is not null then assume that it has changed
    // otherwise just get the List
    if (mTasks != null) {
        mTasks.clear();
        mTasks.addAll(dbhlpr.getAllTasks());
    } else {
        mTasks = dbhlpr.getAllTasks();
    }
    // If the adapter is null then instantiate it
    if (mCustomArrayAdapter == null) {
        mCustomArrayAdapter = new CustomArrayAdapter(
                this,
                R.layout.taskdata_item,
                mTasks);
    }
    // Notify the adapter that the List has changed
    if (notify_dataset_changed) {
        mCustomArrayAdapter.notifyDataSetChanged();
    }
}

Notes

  • This is used to both setup and alter/update the ListView that the adpater is attached to.
  • All that is needed is to call the method at the appropriate place/s in the code e.g. in the Activities onCreate, onResume, after onItemClick/LongClick.
    • e.g in onCreate :-
    • mTaskList = this.findViewById(R.id.tasklist); // The ListView
    • prepTasksCustomArrayList()
    • mTaskList.setAdapter(mCustomArrayAdapter)
    • and in onresume just (in conjunction with the 3 lines in onCreate)
    • prepTasksCustomArrayList()
  • I don't believe that this way is very common, normally you see the adapter being setup in onCreate.
  • mTasks.addAll(dbhlpr.getAllTasks()); is what gets the source data.

Example 2 - Cursor as the Source

/**
 * Build or rebuild the List via Cursor using the bespoke layout
 */
private void prepCursorListBespoke() {
    boolean swapcursor = (mCsr != null && mCursorAdapter2 != null);
    if (mCursorAdapter2 == null) {
        mCursorAdapter2 = new SimpleCursorAdapter(this,R.layout.taskdata_item,mCsr,
                new String[]{ Datasource.TASKS_ID_COL,
                        Datasource.TASKS_TYPE_COL,
                        Datasource.TASKS_NAME_COL,
                        Datasource.TASKS_OWNER_COL,
                        Datasource.TASKS_EXPIRATION_COL},
                new int[]{R.id.task_id,
                        R.id.task_type,
                        R.id.task_name,
                        R.id.task_owner,
                        R.id.task_expiration},
                0);
    }
    if (swapcursor) {
        mCursorAdapter2.swapCursor(mCsr);
    }
}

Notes

  • The prepCursorListBespoke method is used in the same way as for example 1.
  • notifyDataSetChanged could be used instead of swapCursor
    • I use swapCursor because it's more descriptive).
  • However, you can only use swapCursor for Cursors.

Added

The following changes may work, roughly speaking I've applied the List example above.

i.e. Rsults is called by onResume(added) which will notify the adapter that the data has been changed.

The code hasn't been tested as there was insufficient code. (a no code for DatabaseHandler class, and b) no code for DetailsTeacherwebservice class). As such there may be errors.

Look for //++++++++++++++... comments should follow to say what has been done/changed.

public class MainActivity extends Activity {
    int attentedncemarkedCount = 0;
    TextView addteacher;
    DatabaseHandler databasehandler;
    DetailsTeacherwebservice details;
    String emis;

    ArrayList<DetailsTeacherwebservice> addas = new ArrayList<DetailsTeacherwebservice>();
    CustomAdapterTeacherWebservice cusadapter;
    ArrayList<DetailsTeacherwebservice> teacherList;
    private ListView listcontent = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.teacherwebservicemainlist);
        addteacher = (TextView) findViewById(R.id.addteachermenu);

        databasehandler = new DatabaseHandler(TeacherWebserviceMainList.this);
        listcontent = (ListView) findViewById(R.id.teacher_list);
        //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        //MOVED  On ItemClickListener block FROM Rsults to here.
        listcontent.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                                    long id) {


            }
        });
        //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

        teacherList = databasehandler.teacherwebserviceList(emis);
        Rsults();
    }

    
    // Probably don't even need onActivityResult
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                String update = data.getStringExtra("update");
                if (update.equals("1")) {
                    //cusadapter.
                    //CustomAdapterTeacherWebservice adapter = new CustomAdapterTeacherWebservice(this, addas);
                    //+++++++++++++++++++++++++++++++++++
                    // Commented out
                    //listcontent.setAdapter(adapter);
                }
            }
        }
    }

    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Added onResume to call Rsults
    @Override
    public void onResume() {
        super.onResume();
        Rsults();
    }

    private void Rsults() {
        
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        // ADDED 4 lines to see if the notifyDataSetChanged is required
        boolean notifydschg_needed = false;
        if (cusadapter != null) {
            notifydschg_needed = true;
        }
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        // YOU NEED TO GET THE TEACHER LIST AGAIN AS THE DB HAS CHANGED
        teacherList = databasehandler.teacherwebserviceList(emis);
        
        addas.clear();
        //DatabaseHandler databaseHandler=new DatabaseHandler(this);
        //ArrayList<ArrayList<Object>> data =  databaseHandler.abcTeacherNew();

        for (int p = 0; p < teacherList.size(); p++) {
            details = new DetailsTeacherwebservice();
            //ArrayList<Object> baris = data.get(p);
            details.setId(teacherList.get(p).getId());
            details.setTeachername(teacherList.get(p).getTeachername());
            details.setTeachercnic(teacherList.get(p).getTeachercnic());
            details.setTeacherno(teacherList.get(p).getTeacherno());
            details.setTeachergender(teacherList.get(p).getTeachergender());
            details.setAttendance(teacherList.get(p).getAttendance());
            details.setTeacherattendancedetails(teacherList.get(p).getTeacherattendancedetails());
            details.setAttendancedatesince(teacherList.get(p).getAttendancedatesince());
            details.setAttendancetrasnferschool(teacherList.get(p).getAttendancetrasnferschool());
            addas.add(details);
        }
        
        //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        //Just create the adapter and attach it to the listview once
        if (cusadapter == null) {
            cusadapter = new CustomAdapterTeacherWebservice(TeacherWebserviceMainList.this, addas);
            listcontent.setAdapter(cusadapter);
        }
        //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        // Notify the adapter that the data has been changed 
        if(notifydschg_needed) {
            cusadapter.notifyDataSetChanged();
        }
    }
}
Community
  • 1
  • 1
MikeT
  • 32,107
  • 13
  • 38
  • 54
  • Ok let me try Mike –  Jan 11 '18 at 11:30
  • @ArslanAli just noticed. I don't thinnk the check for **mCsr** being **null** is required in `boolean notify_dataset_changed = (mCsr != null && mCustomArrayAdapter != null);` so it should be `boolean notify_dataset_changed = (mCustomArrayAdapter != null);` for the List version. Didn't hurt in my code as mCsr wouldn't be null (the code is from 4 adpaters that switch the ListView to use 1 of the 4) – MikeT Jan 11 '18 at 11:38
  • Ok @MikeT I am trying your solution –  Jan 11 '18 at 11:40
  • If you hit problems then edit the question and I'll look at it tomorrow, just about to hit the sack. – MikeT Jan 11 '18 at 11:41
  • OK sure Thankyou so much for your concern –  Jan 11 '18 at 11:42
  • Your answer doesnt work for me. I have edited the post with all the code Please check –  Jan 11 '18 at 12:45
  • @ArslanAli I've edited answer with code that may work. Definitely getting some shuteye now. – MikeT Jan 11 '18 at 13:58
  • @ArslanAli I think the **Added** in the answer will work as I've done some rudimentary testing (created Databasehandle/DetailsTecherWebservice/invoked update activty) and it works. – MikeT Jan 11 '18 at 22:20
  • Ok let me check this please – Arslan Ali Jan 12 '18 at 10:19
  • Problem that i have been searching solution from 3 days is finally solved by you. You are genius Sir. Thank you so much. You saved my day – Arslan Ali Jan 12 '18 at 10:26
  • @ArslanAli i think you were trying bits but not the whole lot. Great that you've got it. – MikeT Jan 12 '18 at 10:29
0

// you have to set data in adapter in onResume method

 @Override
    protected void onResume() {
        super.onResume();
    }
Mohit Kalia
  • 295
  • 1
  • 6
0

You can use eventbus for this case:
This is an example:

In your build.gradle file

compile 'org.greenrobot:eventbus:3.0.0'

In Activity has listview:

@Override
public void onStart() {
     super.onStart();
     EventBus.getDefault().register(this); // register event
}

@Override
public void onStop() {
     super.onStop();
     EventBus.getDefault().unregister(this); // unregister event
}

@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(EventBusInfo eventBusInfo) {
    if (eventBusInfo.getProcessId() == 99) {
        // eventBusInfo.getData();
        notifyDataSetChanged();
    }
}

In Update Activity

yourButtonFinish.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    finish();
    EventBusInfo event = new EventBusInfo();
    event.setProcessId(99);
    //event.setData(object) //if you want get data back
    EventBus.getDefault().post(event);
    }
});

Class EventBusInfo

public class EventBusInfo {
private int processId;
private Object data;

public EventBusInfo(int processId, Object data) {
    this.processId = processId;
    this.data = data;
}

public EventBusInfo() {
}

public EventBusInfo(int processId) {
    this.processId = processId;
}

public Object getData() {
    return data;
}

public void setData(Object data) {
    this.data = data;
}

public int getProcessId() {
    return processId;
}

public void setProcessId(int processId) {
    this.processId = processId;
}
}
Toan Tran
  • 140
  • 9
0

save the list items in every change then restart the Activity of adapter with intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) to close the old ListView. Is what I do and it works fine. if the list it is not empty, I restart the activity with the adapter and I have a new ListView closing the previous one. Or if it is empty, I start a different activity (to not have an empty ListView) Saving and loading the list items every time to SharedPreferences with Gson For a reason notifyDataSetChanged() does not work good in my case so I prefer this solution.

Amir Dora.
  • 2,358
  • 4
  • 28
  • 47