Update ListView even if its adapter is null - android

I want to update my list even if the adapter is null, but I dont know how to do it.
When I execute the following code my app crashes:
ProductAdapter adapter = new ProductAdapter(context, R.layout.listrow, yal);
if(adapter.getCount()>0){
lv.setAdapter(adapter);
}else{
lv.setAdapter(null);
}
adapter.notifyDataSetChanged();
lv.invalidateViews();

The adapter is sort of the data source for the list. It provides the individual list items. You can't have a list without an adapter, since then you won't have rows in the list.
Check out the ListView documentation for more information.

Don't nullify the adapter - the listView always needs one. Instead, nullify the dataset the adapter works with and make sure getCount() returns 0 if the dataset is null (or empty). Or don't nullify the dataset, but make it an empty list or array.
If your data changes, update the adapters dataset and call notifyDatasetChanged() on the adapter. Do not create a new adapter for the ListView when you get new data. This is important for several reasons; e.g. the listViews position won't jump to the top but stay where it is.
Example:
ProductAdapter adapter = new ProductAdapter(context, R.layout.listrow, null);
listView.setAdapter(adapter);
later
adapter.setData(newData);
adapter.notifyDatasetChanged();

Related

Refresh listview data from custom ArrayAdapter which is populated by SQLite Database

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.

Listview adding to the same line

I have an android app that lists connected clients in a listview, but whenever someone connects, it just adds them to the same line, this is the code I use to add connected client. I am new to listview, and not sure how to do this properly, I looked at the android docs but hard to say what needs to be used. If anyone can help me out that would be great.
remoteip += socket.getInetAddress();
ArrayList<String> addclientlist = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainserverActivity.this,
android.R.layout.simple_list_item_1, android.R.id.text1, addclientlist);
addclientlist.add(remoteip);
adapter.notifyDataSetChanged();
listview.setAdapter(adapter)
I think you need to use this constructor
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainserverActivity.this, android.R.layout.simple_list_item_1, addclientlist);
Also no need to set adapter always just set it once and each time you have to add new item to adapter you can use either of these as in java Array objects are pass by reference, and then call notifydatasetchanged
adapter.add(remoteip);
//or addclientlist.add(remoteip);
adapter.notifyDataSetChanged(); // Dont forget this
You are updating the adapter initialization list, which is futile.
Instead - update the actual adapter:
adapter = new ArrayAdapter<String>(MainserverActivity.this,
android.R.layout.simple_list_item_1, android.R.id.text1, addclientlist);
listview.setAdapter(adapter)
...........
adapter.add(remoteip); // <----- instead of addclientlist.add()
adapter.notifyDataSetChanged();

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();

How to add more item in a list view without refreshing the previous item

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();
}

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.

Categories

Resources