Onyxa
Onyxa

Reputation: 33

Best Practice updating ListView

What I am doing:
The app I'm developing has a clickable list of subjects that is systematically added by the user. At the end of a term/semester, the user clears the list for the next, deleting all the files in which the user values are saved.

The Question:
Is it better practice to simply refresh the activity I am working on by using:

finish();
startActivity(getIntent());

or to clear the list by re-initiating the the ArrayAdapter (named listAdapter) and resetting it using:

listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, R.id.rowTextView);
testListView.setAdapter(listAdapter);

or is there a better way all together?

Upvotes: 3

Views: 434

Answers (2)

Marvin W
Marvin W

Reputation: 444

Generally speaking, closing an Activity is considered bad practice. Activities are heavy on Android, most modern apps don't even use multiple Activities.

If you want to replace the full content or parts of it, you might want to consider using a Fragment. Fragments are parts of UI, that work independently from the Activity. Read more about fragments here: https://developer.android.com/guide/components/fragments.html

If the UI stays the same and you really only want to use a new ArrayAdapter, your approach should work. An even better approach would be to modify the existing Adapter if the data it represents changes.

Upvotes: 1

Ashish
Ashish

Reputation: 183

change the values in array and call adapter.notifyDataSetChanged();

Upvotes: 3

Related Questions