Custom adapter size 0 after notifyDataSetChanged() - android

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.

Related

Adding data at the specific position of Listview in android

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...

Android Listview Item remove but adapter not getting set and item is still showing?

I have this code on Button click
I am removing item from ListView but problem is its still showing on adapter.
restaurants_bean_arr.remove(restaurants_bean_arr.get(item_position).facilities.get(item_position).getUrl());
restaurants_bean_arr.remove(restaurants_bean_arr.get(item_position).facilities.get(item_position).getName());
DisplayDataAdapter adapter = new DisplayDataAdapter(getApplicationContext());
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
Any Idea what could be the problem that item is not getting removing from object class ArrayList.
Thanks
Switch order of:
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
To:
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
It seems like you are creating a new Adapter every time you change your dataset and set this new Adapter to your ListView. You don't have to do that. You can just make changes to your dataset and then call notifyDataSetChanged() on your Adapter.
So your code should look like this:
restaurants_bean_arr.remove(restaurants_bean_arr.get(item_position).facilities.get(item_position).getUrl());
restaurants_bean_arr.remove(restaurants_bean_arr.get(item_position).facilities.get(item_position).getName());
adapter.notifyDataSetChanged();
Here is problem
DisplayDataAdapter adapter = new DisplayDataAdapter(getApplicationContext());
you are creating a new adapter. Create a global value adapter and try like this:
restaurants_bean_arr.remove(restaurants_bean_arr.get(item_position).facilities.get(item_position).getUrl());
restaurants_bean_arr.remove(restaurants_bean_arr.get(item_position).facilities.get(item_position).getName());
adapter.notifyDataSetChanged();

listview not updating with notifydatasetchanged() call

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.

Android ListView setAdapter issue

I'm trying to bind data do a listview on android, but I'm not able to.
I saw some code on the internet and it worked, but I just don't know why, and I don't want to create a new ListView on the fly, I want to use the one that is listed on the main.xml
Why I can do this:
ListView lv = new ListView(this);
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,PEOPLE));
setContentView(lv);
But I can't do this:
ListView listPessoas = (ListView) findViewById(R.id.listPessoas);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.listPessoas, PESSOAS);
listPessoas.setAdapter(adapter);
What is R.id.pessoas??
This works fine for me:
String[] x = new String[]{"AAA","BBB","CCC"};
ListView lv = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> test = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,x);
lv.setAdapter(test);
Your error here is when you are creating the new ArrayAdapter. You are passing R.id.listPessoas as the row view to use for each row. This is the id of the ListView. The adapter is looking for a layout id containing a text view to be used for each row of the list. Change the R.id.listPessoas to android.R.layout.simple_list_item_1 and your code should work. The simple_list_item_1 layout is just a TextView that the data will be bound to.
What you have written is not correct, the second parameter in the array adapter constructor should be the simple layout for each list item not again your list view.
If your have a custom complex layout for your list item you need to write a custom adapter as well.
If you are getting error.
getApplicationContext(); will work instead of 'this'.

How do you add array to listview

I have an array of apps(PInfo) and I am wondering how do I add that array to a listview?
ArrayList<PInfo> info = appsGetter.listPackages();
int number = 0;
PInfo appInArray;
while(number < info.size()){
appInArray = info.get(number);
}
This is what I have at the moment, the listPackages() is a method that is getting the names of the apps from the device.
At the moment I am trying to get the information out of the array one by one and add it to the listview like that. Is that how I should do it our should I add the array straight to the listview? And how do you do that?
You can use an ArrayAdapter and initialize it like this:
ArrayAdapter<PInfo> adapter = new ArrayAdapter(context,
android.R.layout.simple_list_item_multiple_choice,
info);
Then you can you use ListView.setAdapter(adapter).
I'm not sure if this is what you're asking though. So please clarify further if this is not what you're asking
Try using an Adapter. For example (using just the String value of an object) you could do the following:
ListView listView = (ListView)findViewById( R.id.myListView );
final ArrayList<String> listItems = new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, listItems );
listView.setAdapter( adapter );
Just a quick example, but I hope it gives you a starting place. Just make sure if you add values to your data source later (in this case the ArrayList) to call the adapter's "notifyDataSetChanged()" method so that it can be properly reflected in whatever has been bound to the adapter (in this case the ListView).
You need to use an ArrayAdapter. Just search for a ListView and ArrayAdapter sample online. It's quite simple once you see it done.

Categories

Resources