I have a GridView backed by an ArrayAdapter. Everything works except for one thing: I couldn't clear that adapter (need this when refreshing the `GridView).
This is what I have:
adapter.clear();
adapter.notifyDataSetChanged();
Prior to this project I have never had any issue regarding adapter clearing. I can't find what I am doing wrong here.
Any idea? (don't hesitate to ask for specific details).
Thanks!
Problem solved.
I had to keep a reference on the collection that the adapter was taking its data from and then do:
myCollection.clean();
adapter.notifyDataSetChanged();
Related
Sorry for a brick of a code but I read that I need to run something on UI thread, how do I do that? private ArrayList JsonFIveDays(String weatherSearchResults) is where I set my listview adapter if that helps.
You have to notify the adapter whenever you do changes in the weatherArrayList.
For Example,
weatherAdapter.notifyDataSetChanged()
But in your case to use like that you have to declare the weatherAdapter as a property of the class.
I hope it will help you.
You need to notify adapter that you changed data with notifyDataSetChanged(). You can do that in setData method after changing data.
I'm starting learning Android and I want to know if there is some option in Android that let you modify each item or view (I don't know how it's named exactly, I mean each of the items from an ArrayList that you show in a ListView).
Well, I made a ListView that is going to show some books that were located in an ArrayList named "books".
I made a custom adapter that I associate to the ListView to show each item with the corresponding layout in the application. I also have a class "book" for each item that is going to be shown in the ListView.
Further, I made an Intent that I call from MainActivity with startForActivityResult(), that I process in Book class and that I return to MainActivity with all data of a book with the method setResult and got the information with onActivityResult() and the requestCode.
So I don't have any problem to add items to the ListView, just I have the problems if I want to modify some of the items (or views) that are located in the ListView (for example if I have title and author of a book, if I put some wrong information, I want have the option to change it).
I have that, in the same moment that you click on some of the items of the ListView, a new layout will be show to modify the information that it's wrong so I use the method setOnItemClickListener with onItemClick event on the custom adapter that I created before. Here it's where I call the new Intent to modify the wrong information with the method startActivityForResult().
I made the same as before to add a new item but, instead of add a new item with custom_adapter.addBook(title,author) I want to know if there is some option to made something like this: custom_adapter.modify(title,author) or custom_adapter.update(title,author), I mean, when you have modify all the items that were wrong of a book (for example an EditText that were "title") and you have all the information in the MainActivity class (because you returned it with setResult), how to put it again in the same item updating it in the custom_adapter and also in the ArrayList.
I searched it on the Internet but I didn't find anything.
I'm sorry if I have a poor English, but I expect that it can be understand.
Thank you very much!
If I am understanding your problem correctly - you could simply modify the ArrayList of type Book that is backing your ArrayAdapter.
So if you know what Book object you want to modify then you can simply make your changes to the Book object itself. As long as this Book is a reference to the same object that you originally added to the ArrayList you instantiated your ArrayAdapter with then you can then call custom_adapter.notifyDataSetChanged() to tell the adapter to redraw its childviews with the new data.
There are some good code samples on the Internet but you have to understand the code for your purposes. So...here is a start, look at Using an ArrayAdapter with ListView. The code shows the use of ArrayAdapter with getView() method. And I hope it shows how to define the listeners, which you need.
How about that for a start? Have fun...
Thank you very much for all help you gave to me. I'm very pleased with you. :)
Finally, I just send the info with a Bundle when I started the Intent, also with the position. And after, I just used this position to set the new info to my items (in my case, books).
Again, thank you very much ;)
I am really desperate, please help me.
In my main Activity I want to remove all items from the listview then add other data. I am able to delete the data and refresh the listview, but when I add new data, something strange happens. I insert the data in my Array. I call notifydatasetchanged on my adapter after every insertion. The adapter gets those items (I have confirmed this in debug mode). But the listview will not refresh. And now comes the VERY STRANGE thing: When I tap the back button on my device, the application quits (this part is ok), but for a moment before quitting it refreshes the listview with the correct data.
Here are some parts of my code:
For deleting the data I use:
newsArrayList = new ArrayList<NewsClass>();
Adapter = new NewsAdapter(this, newsArrayList);
lv.setAdapter(Adapter);
"lv" is my listview. The listview correctly loses all items and refreshes. I have also tried this:
newsArrayList.clear();
Adapter.notifyDataSetChanged();
Also worked like expected. Then I use a "for" loop to add new items to my array, where I have the following lines:
newsArrayList.add(news[i]);
Adapter.notifyDataSetChanged();
As I wrote, when running in debug mode, everything seems fine. I have spent all day working on this problem. I have read several posts about similar issues on stackoverflow, I even watched a video on youtube. I am calling everything from the main UI thread.
I can work around this problem by restarting my activity, but I want to do it better than that. I have tried invalidate and such things, but nothing helped. Please note the VERY STRANGE thing, I wrote...
I think, that I have to force the activity to redraw its views, or something like that, but even refreshDrawableState(); on my listview did not help.
Thank you in advance!
Basically If you want to clear list data then just call
newsArrayList.clear();
lv.setAdapter(null);
Again add what ever your data in ArrayList and set adapter
newsArrayList = new ArrayList<NewsClass>();
Adapter = new NewsAdapter(this, newsArrayList); // considering list with some elements
lv.setAdapter(Adapter);
If you are getting outofmemory error/exception then there is problem with your Adapter Class inside getView method
I'm sorry to ask about what seems to be a simple problem but I've been looking for a solution for hours and now I can't stand it anymore.
My app is very simple. I've got "tasks" in a SQLite db, and I populate a listView with this db like this :
_dataAdapter = new TaskAdapter(_c, _taskDAO.getTasks());
_listTask.setAdapter(_dataAdapter);
which returns an arrayList of Task.
Problem is, when I click the dialog to remove a task, it removes it from the db but it's still on the screen, that's why I need to refresh the listView.
case DialogInterface.BUTTON_NEUTRAL:
_taskDAO.removeTask(1);
_dataAdapter.notifyDataSetChanged();
break;
But it does not work. I think it's due to the fact that I remove the task from the db but not from the adapter but I'm not quite sure.
Anyway, if someone's got a solution ...
If your adapter is an ArrayAdapter or BaseAdapter, the data source of your ListView is not the DB itself but the List you provide to the adapter.
You must update the datasource of your adapter (give him a new list) then call
_dataAdapter.notifyDataSetChanged();
Another solution would be to recreate the adapter :
_dataAdapter = null;
_dataAdapter = new TaskAdapter(_c, _taskDAO.getTasks());
_listTask.setAdapter(_dataAdapter);
Hope it helps
try,
(listName.getAdapter()).notifyDataSetChanged();
i have a listview with a custom arrayadapter. each row of the list have also a gallery with her own adapter. so everything works fine. but scrolling fast the list, it stagnates because each row must load the the secound adapter of the gallery. this are only millisecounds, but you feel ist, because the list don't scroll smooth. so i would like to load the secound arrayadapter of the gallery in an asynctask, so that the scrolling in not influence. but their is the problem:
in the doInBackground-method i want to do this:
CustomAdapter customAdapter = new CustomAdapter(activity, R.layout.items, arraylist);
but there he throws me a futuretask error. i thing it is the "activity" that is the problem. can i solve it? or is their no chance?
I hope you can help me.
Thanks!
I think that the problem is that you are using non-reusable ListView adapters. Using them will improve application speed as it will reuse resources and you will avoid garbage collecting. Find out more at: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/, especially look at OrderAdapter implementation.
You won't be able to modify view in doInBackground as it is not invoked on the UI thread. Please refer to Android documentation: http://developer.android.com/reference/android/os/AsyncTask.html