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...
Related
I have a ListView, an ArrayList and an MaterielAdapter that extends ArrayAdapter. My problem is that calling notifyDataSetChanged() won't update my ListView... I spent 3 hours trying all similar issues on SO but none was efficient for me.
private ListView lvMateriels;
private ArrayList<Materiel> materiels = new ArrayList<>();
...
lvMateriels = new ListView(this);
lvMateriels.setAdapter(new MaterielAdapter(this, R.layout.object_line,materiels));
...
materiels.add(new Materiel());
((MaterielAdapter)lvMateriels.getAdapter()).notifyDataSetChanged();
My collection has all the objects but listview never refreshes, nor the adapter...
You're probably not even adding the ListView to the activity view. Instead of doing new Listview(this), I'd recommend you to define a listview in your layout xml
Something like:
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Then in your activity:
lvObjects = (ListView) findViewById(R.id.listview);
Found out solution
My problem was that I was setting my ArrayList another ArrayList
materiels = getMateriels() // getMateriels() returns an ArrayList<Materiel>
It seems that setting a new object to the list doesn't work, what I finally did is the following :
materiels.clear();
materiels.addAll(getMateriels());
And by the way for those who need to have a header into their ListView, you have to do like :
((YourAdapter)((HeaderViewListAdapter)yourListView.getAdapter()).getWrappedAdapter()).notifyDataSetChanged();
Stock your adapter, and your adapter will be your list too.
MaterialAdapter adapter = new MaterialAdapter(this,
R.layout.object_line);
lvObjects = new ListView(this);
lvObjects.setAdapter(adapter);
...
adapter.add(new Object());
adapter.notifyDataSetChanged();
Currenlty your adapter, do not know that he need to use the list that you pass in argument. But you extends ArrayAdapter, that means that your adapter is a list.
I have a custom ArrayAdapter which is set to a listview with data from database:
CustomAdapter adapter = new CustomAdapter(myview.getContext(), -1, database.getAll());
listview.setAdapter(adapter);
Now, I want when the user deletes a database item the listview should refresh.
Until now, I can successfully delete the item from the database, but it remains in the listview. In the ArrayAdapter class I call refresh() method from a new class same to adapter's parent class
public void refresh(){
ParentClass class_par = new ParentClass();
class_par.refreshfromParent();
}
And in the ParentClass I have a new method: refreshfromParent()
public void refreshfromAlarm(){
DatabaseHandler database = new DatabaseHandler(myview.getContext());
listview.setAdapter(new CustomAdapter(myview.getContext(), -1, database.getAll())); // here I change listview adapter, and populate it with new database data, but I get NullPointerException
}
So, how can I refresh listview items? Please, DO NOT mention notifyDataSetChanged(), it DOES NOT work.
here you making en error
CustomAdapter adapter = new CustomAdapter(myview.getContext(), -1, database.getAll());
listview.setAdapter(adapter);
//so first you should make an arrayList and define it globally like
// ArrayList <yourType>list = new ArrayList<>();
//then initialize your adapter like this
list.addAll(databse.getAll());
CustomAdapter adapter = new CustomAdapter(myview.getContext(), -1, list);
listview.setAdapter(adapter);
// now when you want to delete an item
// delete it from your list like list.remove(location)
// and also delete same item from your datebase
// after that just call this
notifyDataSetChanged();
I realized that notifyDataSetChanged is a big trouble, so I decided to close and reload my fragment which contains my listview. Thus, the result looks the same with DataSetChanged and there is no difference in the users' eyes. I suggest everyone who reaches in this question do the same.
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
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();
}
This is my code
listview =(ListView) findViewById(R.id.lv1);
ArrayList<SClass> Monday = new ArrayList<SClass>();
SClass s1=new SClass();
s1.sName="samp";
s1.salary=1000;
Monday.add(s1);
temp=Monday;
adapter = new CustomAdap(this, temp);
listview.setAdapter(adapter);
The above code works fine.But when i change my code to this
listview =(ListView) findViewById(R.id.lv1);
adapter = new CustomAdap(this, temp);
SClass s1=new SClass();
s1.sName="samp";
s1.salary=1000;
Monday.add(s1);
temp=Monday;
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
Listview does not show anything.what is the problem?
It looks like you're changing the collection that you initialized adapter with. I would change your code in this way:
// initial setup
listview =(ListView) findViewById(R.id.lv1);
ArrayList<SClass> Monday = new ArrayList<SClass>();
adapter = new CustomAdap(this, Monday);
listview.setAdapter(adapter);
// change your model Monday here, since it is what the adapter is observing
SClass s1=new SubjectClass();
s1.sName="samp";
s1.salary=1000;
Monday.add(s1);
// notify the list that the underlying model has changed
adapter.notifyDataSetChanged();
Note that if your CustomAdap was a subclass of ArrayAdapter, you could also have done
// change your array adapter here
SClass s1=new SubjectClass();
s1.sName="samp";
s1.salary=1000;
adapter.add(s1);
// notify the list that the underlying model has changed
adapter.notifyDataSetChanged();
EDIT: I understand more what you want to do now thanks to your comment. You'll probably want to have the adapter replace its contents with that your different ArrayLists then. I would make your CustomAdap be a subclass of ArrayAdapter.
Then you can utilize it this way:
// replace the array adapters contents with the ArrayList corresponding to the day
adapter.clear();
adapter.addAll(MONDAY);
// notify the list that the underlying model has changed
adapter.notifyDataSetChanged();
Why it works in first code ?
--- Because you are setting the values to temp List and passing it the adapter and it shows it into listview.
Why not work in second code ?
--- Because you are setting temp to adapter far before you set value into temp
second,your adapter class might not getting the updated value when you set new value to temp ..that because temp is not public or not at class level or not static..
Put the temp declaration at root level and try.
And please show your full code as much as required and Logcat if you getting any warnings than also.
Check for a link to your referenced view in the proper xml file. Or at least check for the existence of said xml file.
What adapter are you using? It is clearly a case where your adapter is not getting updated after u set the data in your temp variable.