How to refresh ListView in Android - android

I know you can call notifyDataSetChanged(); How would you do that in this context -- is it even possible?
ListView listView = getListView();
listView.setTextFilterEnabled(true);
setListAdapter(new ArrayAdapter<String>(Items.this, R.layout.list, items));
Edit: I have changed to one of the suggestions below. Here is what I am doing:
protected void onPostExecute(Void v) {
adapter = new ArrayAdapter<String>(Items.this, R.layout.list, items);
setListAdapter(adapter);
adapter.notifyDataSetChanged();
}
Here I am refreshing inside an AsyncTask method. This is being called inside a Dialog with a Submit button on top of a ListLiew. The Dialog inserts new data into a database and I want the list refreshed where to reflect this. I have tried this code above AND inside the onClick for the Button in the Dialog. Right before dialog dismissed.

You need to initialize the ArrayAdapter by itself:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Items.this, R.layout.list, items);
setListAdapter(adapter);
...then call notifyDataSetChanged() on that:
adapter.notifyDataSetChanged();
Note that you only need to call this method if the data actually changes; given this, you probably need to have it stored as a member variable in the class so it can be accessed later.

You should just be able to call notifyDataSetChanged() on your ArrayAdapter, since ArrayAdapter extends BaseAdapter
http://developer.android.com/reference/android/widget/ArrayAdapter.html
In your example you'd do something like this, taking that you meant to call listView.setListAdapter()
(ArrayAdapter)listView.getAdapter()).notifyDataSetChanged();

In your example, you shouldn't be setting the adapter in your AsyncTask. Generally it is a good idea to do that in onCreate() because otherwise you are setting it each time AsyncTask is called. If you make your adapter a member variable, then you should be able to call <adapter>.notifyDataSetChanged() from within the AsyncTask.

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.

Android, How do i Update the selected item in my listview from my database?

Here's my code so far, but the application crashes when I press the update button.
I want to update a selected item in my list, I have already created the update activity that will allow me to load the values on my database but I can't figure out how to load the value of selected item in list.
{
ArrayAdapter<String> ard=new ArrayAdapter<String> (this,android.R.layout.simple_list_item_single_choice,list);
lv.setAdapter(ard);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
btnupdate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
SparseBooleanArray sba = lv.getCheckedItemPositions();
Intent intent = new Intent(HomeworkInfo.this, UpdateHomework.class);
startActivity(intent);
finish();
}
});
}
When an ArrayAdapter is constructed, it holds the reference for the List that was passed in. If you were to pass in a List that was a member of an Activity, and change that Activity member later, the ArrayAdapter is still holding a reference to the original List. The Adapter does not know you changed the List in the Activity.
Your choices are:
Use the functions of the ArrayAdapter to modify the underlying List (add(), insert(), remove(), clear(), etc.)
Re-create the ArrayAdapter with the new List data. (Uses a lot of resources and garbage collection.)
Create your own class derived from BaseAdapter and ListAdapter that allows changing of the underlying List data structure.
Use the notifyDataSetChanged() every time the list is updated. To call it on the UI-Thread, use the runOnUiThread() of Activity. Then, notifyDataSetChanged() will work.
I hope this helps you ..
Happy coding !!

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

Call a method from an activity from an adapter

I am working with an adapter which has several buttons on each item of the ListView. I have, for example two buttons. One for save and another for delete.
When I click on them, I want the adapter to refresh. I have a method on my activity that does that: Update the adapter. I made it since I couldn't make the Notify methods of the adapter work.
I am sending the Activity to the adapter as a parameter and then call the update method from the adapter since I am setting the OnClick methods of each button in the adapter.
The problem is that it doesnt seem to work always. I am guessing it has to do something about the cycle of the activity and Android cleaning memory however I can't seem to understand where is the problem.
I am aware this is some ugly way of doing it, so what should be the best way of doing this?
Summary: I need to refresh the adapter from the adapter itself when I delete an item of the ListView or update it (BaseAdapter)
Edit:
I am adding some code. This is my constructor of my adapter:
public SpendingPlanAdapter(Context context, SpendingPlan sp, int month, int year, money.activities.SpendingPlan act)
Inside the adapter I have an OnCLickListener attached to a button. I am calling the update method like this (Result is the result of a database insert):
if (result){
Toast.makeText(context, context.getResources().getString(R.string.succesfully_deleted), Toast.LENGTH_LONG).show();
act.updateAdapter();
} else {
Toast.makeText(context, context.getResources().getString(R.string.oops), Toast.LENGTH_LONG).show();}
And this is my UpdateAdapter method:
public void updateAdapter(){
SpendingPlanAdapter adapter = new SpendingPlanAdapter(getApplicationContext(), currentSp, currentMonth, currentYear, this);
limitList.setAdapter(adapter);
}
You can call updateAdapter() like this
(SpendingPlan (context)).updateAdapter();

android auto refresh listview item

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.

Categories

Resources