Is there anyway to continuously load data on one list view? I tried doing this but the second setadapter overrides the first one. Here is my code:
ArrayAdapter<Display> adapter = new DisplayAdapter(getActivity(), R.layout.display_list_item, response.container.InactiveDisplay);
existingItemsListView.setAdapter(adapter);
ArrayAdapter<Display> adapter1 = new DisplayAdapter(getActivity(), R.layout.display_list_item, response.container.ActiveDisplay);
existingItemsListView.setAdapter(adapter1);
An alternative solution:
List<MyAdapterItems> myItems =
((MyAdapter)myListView.getAdapter()).getMyAdapterItems;
/*
*You can use myitems.add(Object o) method to add items then you can call
*/
myListView.setAdapter(myItems);
add data to first adapters array and then call adapter.notifyDatasetChanged(); to notify listview that there is new data to load
Related
I have DAO method returned LiveData<List<Category>>:
LiveData<List<Category>> listLiveData = categoryDao.getAll();
After that I need set this data to my Adapter:
listLiveData.observe(this, categories -> {
if (categories == null || categories.isEmpty()) {
price.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.INVISIBLE);
} else {
categoryAdapter = new CategoryAdapter(categories);
categoryAdapter.setOnItemClickListener(new ClickHandler());
recyclerView.setAdapter(categoryAdapter);
}
});
if I have not data in DB I show button for update(get data from server and insert to DB).
if I have data I create adapter and set data to it.
After that if I insert some Category to DB I get this:
triggered observe method and get all categories
create new adapter with this data
I think it is very bad practice(create a new adapter for update each item) and I can some way:
Change my adapter and add method addData(List<Category> categories);
in onCreateView I create adapter: categoryAdapter = new CategoryAdapter(new ArrayList());
then when I get data in observe method I add it to adapter:
adapter.addData(categories);
and into addData method in loop check each item and if not exist add to list and notify data.
change method
LiveData> listLiveData = categoryDao.getAll();
to
LiveData<Category> category = categoryDao.getLast();
and add this item to observe method. But I have 2 problems: 1 - How can I add firstly all data? I must implement 2 methods getAll(call wen create adapter) and getLast(call for each insert in DB). 2. How can I write this getLast method?
correct way that you will advise me :)
Your first step is right, you should create adapter only once in a lifecycle of Fragment or Activity and set the value(list) to adapter whenever you need to change dataset.
You should use getAll() method and it is best practice because you have data in local database and that doesn't take longer. If you have long data set then you should use pagination where only limited no. of items are fetched at a time. That can be achieved using LIMIT clause queries in sqlite.
And then use notifyDatasetChanged() method. If you want to show a smooth animation for newly inserted items use DiffUtil class to compare list and notify adapter accordingly.
To know how to use DiffUtil, check this out https://medium.com/#iammert/using-diffutil-in-android-recyclerview-bdca8e4fbb00
Hope this helps.
In Base adapter, I have used some imageview, can I change the image src in activity?
You should not access the adapter views directly in activity. Write a method in adapter instead. Call adapter.change image from your activity.
public void changeImage(int imgResId) {
likeButton.setImageResource(imgResId);
}
If Image src is provided from activity then you can call notifyDataSetChanged() after changing data.
Eg.
adapter = new MYAdapter(data);
list.setAdapter(adapter);
//change data here
adapter.notifyDataSetChanged();
You need to update the model data which is associated with ViewHolder after that just notify the adapter and adapter will update your item with new model data.
Take example if you have Array of 10 objects and you want to update ImageView on 5th position then update your 5th model in ArrayList and call adapter.notifyDataSetChange() it will update desired image view.
I have a listview and I have to add two more element in listview. I have to add notifyDataSetChanged() . but not sure how to do it.
listView = (ListView) findViewById(R.id.my_listview);
myListAdapter = new MyListAdapter (this, sourceList);
listView.setAdapter(myListAdapter);
myListAdapter.notifyDataSetChanged();
sourceList is the List .
I need to add 2 items at the end of the Listview. Thanks.
Just add your desired data to the adapter. For example:
myListAdapter.add(obj1);
The newly added data will be attached at the end of your ListView.
Remember: when injecting data directly into an Adapter, you don't need to call its notifyDataSetChanged method.
If you modify your sourceList object, in that case its necessary to call notifyDataSetChanged method in order for the Adapter to refresh its contents.
Check out this Dummy Example to add the adapter to the List.
Read the Comment
ListView lstViewtodo; //Your ListView
List<NoteModel> list_note_model = new ArrayList<NoteModel>(); // for Adding the Array of your Model
NoteModel noteModel = new NoteModel(txttitle, jsonArray, obj.getUid(), txtColorType, false); //Your Model
list_note_model.add(0, noteModel); //Adding the value to array
lstViewtodo.setAdapter(list); //finally adding the content to list
To Add the Content Dynamically...
NoteListAdapter list = new NoteListAdapter( NoteMainActivity.this , R.layout.todo_adapter_view , list_note_model); //Your class extending the ArrayAdapter<NoteModel>
NoteModel noteModel = new NoteModel(txttitle, jsonArray, obj.getUid(), txtColorType, false);'
list_note_model.add(0, noteModel);
list.notifyDataSetChanged();
Let me Know if You Difficulty...
I have a listview in which i am adding some data after fixed interval.but I don't want to set the adapter again as it will refresh the complete list.Is there any method to add item without refreshing the complete list.
Thanks in advance.
You probably 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's RecycleView
You can call adapter.notifyDataSetChanged() to just update the list.
Adapter's getView() is called at different times and there is no particular pattern. So your views are updated whenever ListView wants it to be updated.
But as far as I see it, 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 to bounce back to first item on the list.
Hope that helps.
you can use this .notifyDataSetChanged()
However notifyDataSetChanged() only works For an ArrayAdapter,if you use the add, insert, remove, and clear functions on the Adapter.
You can use
adapter.add(<new data item>); // to add data to your adapter
adapter.notifyDatasetChanged(); // to refresh
For eg.
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 whenever you have new data to be added to list you can do following
private void appendToList(ArrayList<String> newData) {
for(String data : newData)
adapter.add(data);
adapter.notifyDatasetChanged();
}
First I setup a ListView adapter
this.chapters = new ArrayList<String>();
ListView lvChapters = (ListView) findViewById(R.id.mangaChapterList);
lvChapters.setAdapter(new ChapterListAdapter(context, R.id.lvChapters, this.chapters));
After that, I popular some data into "this.chapters" in an AsyncTask class. But the listview still empty??
Show the code for your ChapterListAdapter, are you sure you are calling the registered observers?
If ChapterListAdapters is extending an ArrayAdapter, then you should call notifyDataSetChanged.
public void notifyDataSetChanged ()
Since: API Level 1
Notifies the attached View that the underlying data has been changed and it should refresh itself.
Reference your adapter through any variable like this
ChapterListAdapter adapter = new ChapterListAdapter(context, R.id.lvChapters, this.chapters);
and then set adapter.
pters.setAdapter(adapter);
and then just call adapter.notifyDataSetChanged(); after you add the data to the this.chapters.