How to add an additional item to the list without updating the previous item

I have a list in which I add some data after a fixed interval. But I do not want to install the adapter again, as it will update the complete list. Is there a way to add an item without updating the full list.

Thanks in advance.

+6
source share
4 answers

You might want to use the following (in RecycleView not ListView ):

 notifyItemInserted(0);//NOT notifyDataChanged() recyclerViewSource.scrollToPosition(0); //Scroll up, to use this you'll need an instance of the adapter RecycleView 
0
source

You can call adapter.notifyDataSetChanged() to just refresh the list.

The getView() adapter is called at different times and there is no specific template. Therefore, your views are updated every time the ListView wants it to be updated.

But as far as I can see, you are looking for adapter.notifyDataSetChanged . The workflow should be something like this.

 Set adapter to ListView Add data to adapter` Call notifyDataSetChanged() on adapter. 

It will at least prevent your list from returning to the first item in the list. Hope this helps.

-1
source

you can use

 adapter.add(<new data item>); // to add data to your adapter adapter.notifyDatasetChanged(); // to refresh 

For instance,

 ArrayAdapter<String> adapter; public void onCreate() { .... .... ArryList<String> data = new ArrayList<String>(); for(int i=0; i<10; i++) { data.add("Item " + (i+1)); } adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data); setListAdapter(adapter); } 

Now that you have new data to add to the list, you can do the following

 private void appendToList(ArrayList<String> newData) { for(String data : newData) adapter.add(data); adapter.notifyDatasetChanged(); } 
-1
source

you can use this. notifyDataSetChanged ()

However, notifyDataSetChanged () only works for the ArrayAdapter if you use the add, insert, delete, and clean functions on the adapter.

-1
source

Source: https://habr.com/ru/post/921179/


All Articles