Android listview refresh only once - android

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

Related

Android: App response gets slow after several operations

I am developing a POS app for a restaurant. Things work fine, except that after some time the app gets very sluggish. The response appears to be 2 seconds late, and then we need to restart the application.
Here is how it looks like:
Each time when a food category is selected, e.g COLD DRINK, I remove the previous food items below it and add new items according to the selected category.
So, to me it seems that when I frequently add and remove views from the panel (GridLayout), the app gets slower and slower.
I simply do gridLayout.removeAllViews(); to remove all views.
Can anyone tell me how to resolve this please?
You are removing the views, but it don't necessary destroy then, so they continue occupying memory resources.
It's better to use some kind of list with an adapter, like ListView or RecyclerView, witch you can add or remove the views and they will only occupy memory when showing (or all that is showing more two).
You can do something like :
ArrayList<String> listItems = new ArrayList<>();
listaItems.add(yourString); // call that ever you have to add new itens
ArrayAdapter<String> itemsAdapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(itemsAdapter);
to remove do something like :
listaItems.remove([INDEX]);
itemsAdapter.notifyDataSetChanged();

SwipeMenuListView new Row added is showing First time data

I've read many of the previous posts on this topic, but i'm not getting it right.
I have an adapter which has a private values of the items in the list.
When i update the values(add a new items), i watch the value in debugger and the "getView" func and see that the value is correct.
BUT the actual rowView i see is just the first item in the list.
I have no clue what may cause this.
This listview is on the same activity while i show a different layout and hide the listview to add a new item.
Can there be a connection while the listview visibility is "GONE"?
When i remove items from it it updates listview fine(that is done when listview is visible).
private void updateAdapter() {
this.values.clear();
this.values.addAll(staticlistIndifferentclass);
notifyDataSetChanged();
}
~~~~UPDATE~~~~
Ok,
So i discovered the cause of the problem, though i'm not sure why it is.
The code was fine the way it was with regular Listview but the bug is on:
com.baoyz.swipemenulistview.SwipeMenuListView
Try use ListView.invalidateViews
This should cause views rebuilding
Use new Adapter object like this :
listView.setAdapter(new yourCustomAdapter());
When you delete all dataSet items ,it is better to bind new Adapter object .

notifyDataSetChanged with SQLite

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

Android ListView - Refresh and sort?

I am doing this:
// Member variable
List<String> items = null;
// in onCreate
items = new ArrayList<String>();
// later on in a Task (onPostExecute)
items.add(NewItem);
adapter.notifyDataSetChanged();
My List is in Alphabetical order. When I add an item, it simply puts it at the bottom. Any way to put it in the proper place?
(Yes it will be in proper order the second time you come back to this page, but not on adding it)
Edit: I am first putting it in order through a SQL script call.
My suggestion to your answer is that, as and when you add an item on ListView try to set the adapter again on the list at the time of adding item.
this way you get the refreshed data on your ListView.
I decided to research any methods for ArrayList and did not find any. I did find this, however:
items.add(NewItem);
Collections.sort(items);
Worked perfectly! I had to import Collections into the project.

how to stop executing the getView() method while scrolling the ListView items using SimpleAdapter in android

i have used SimpleAdapter to display a list of items in a listview.While displaying the getView() is called every time to display every list item with different layout.Here i got succeeded in doing so.But here the problem is whenever i scroll the list items again the getView() method is executing and iam getting the unreliable results.Here my requirement is i dont want to execute the getView() method while scrolling the listview.Any body please guide me out of this situation.
This is the intended behaviour of the ListView, it is automatically buffered. So as you scroll, new items are inflated and added and the old ones are knocked off. This mechanism works by calling your overridden getView() method.
If you don't want this then just use a LinearLayout inside a scrollview and manually populate it with your list items. You can even still use your adapter by using:
linear.addView(adapter.getView(index, null, linear));
Hope this helps!
I want to add one thing that if
you have more than one records in your adapter just use a for loop like this.
ArrayAdapter<Question> adapter = MyCustomArrayAdapter
.GetArrayAdapterInstance(this, flag1, flag1, lst_question);
for(int i=0;i<adapter.getCount();i++)
{
linear.addView(adapter.getView(i, null, linear));
}
I also get the same problem, and here is our response !
Android, how to stop reading getView() function again, if the content is already downloaded in the ListView
Let me know if it work for you ;)

Categories

Resources