Android recreating ListView - android

How can I recreate listView?
I have listView with items containing few TextViews and I want, to reaload ListView and hide part of those TextViews from every row. I tried setting new adapter, clearing adapter (it makes listView empty), invalidating listView, using notifyDataSetChanged(). Nothing forced ListView to recreate items.

Use a ArrayAdapter backed by an ArrayList. To change the data, just update the data in the list and call adapter.notifyDataSetChanged() - Robby Pond
Also please check Google I/O 2010 - The world of ListView

Related

does ListView re-draws itself every time the adapter changed?

I want to make a list of SMSs in my own application..
my question is about the steps makes by the adapter (automatic)
-when new message arrived I adds it to list object (not the ListView).
-then I passes the list to the adapter of the listView.
the adapter GetView() method run for every Item in the list
-I notify the listview about the change.
- the listview re-draws all its existing rows and then draw the new row.
My question: this behavior (re-draw and redraw, it mean every row will be drown times equal to the total rows) affect the performance?
*if the question is not clear I say: does the ListView Draw all the Raws just to add new row? *
The ListView doesn't redraw every single item in order to add one. It will only be drawn when you scroll to it. And yes you have to notify ListView that the change had happened.
I guess you want to add the item at the top position. So all other item's position and index will change . Adapter will redraw the whole list(only elements which are visible on the screen) .
For performance you can use viewHolder pattern.
see this link
[Making ListView Scrolling Smooth][1]http://developer.android.com/training/improving-layouts/smooth-scrolling.html

Is possible refresh a item from a ListView without refresh entire list?

I have a listview with many items(10 - 200 approx.), when I click on an item, I update its attributes and then update the listview. But as many items the update is a bit slow.
Is there anyway to only update an item in a listview without having to refresh the entire list?
Regards
It is possible to change an element of a ListView, but it is not a good solution, because you are accessing a widget of your listview item directly e.g. textview. But it at least can be used as a temporary solution
Example:
How can I update a single row in a ListView?

Android: delete item from ListView

I have a problem with deleting items from ListView.
I am using a subclass of BaseExpandableListAdapter.
The problem is that when I am deleting an item, I change the underlying data, after which I call notifyDataSetChanged. All seems ok. But the refresh of the ListView does not happen immediately. So, if somebody keeps checking/un-cheking some of my items, they will point to data that there is no longer in adapter (but they are still displayed in the ListView).
Example :
Say I have a ListView with 3 views and 3 items in the adapter (1, 2, 3):
I select item 3 and press delete. Now they are 3 views and 2 items in the adapter
Call notifyDataSetChanged (note that the ListView still has 3 views since it did not had time to refresh)
I keep selecting the 3rd item which will query my adapter for an item which is no longer there
My question is how do I deal with this situation ? It seems to be a gap between the time the notifyDataSetChanged is called and the ListView is refreshed and within that gap, we need to check that all the requests received in the adapter are still valid.
Try to delete your list item like.
filelist.remove("your selected item position".intValue());
This may solve your problem.

How canI dynamically add items in a list view and swap them accordingly

I am trying to create a list view, in which items can be added dynamically, the dynamic part is working fine, as it can be done using simple cursor adapter and inflating a layout with the list item, each time an item is created. But now the problem I am having is that, I want to swap these items as well, swapping as in replacing item positions. All the examples I have seen use a string array, that is a predefined list. How can I achieve this?
Use an Arrayadapter as your Listadapter. You can now use insert(object, int) to add an item to a specific position in the lists dataset. With remove items can be removed from the lists dataset. Since the Arrayadapter will monitor changes to the dataset itself the list should update once you are done modifying the Adapter.
If you need to use a CursorAdapter this may get harder. You would need to change the underlying database and then requery the Cursor that is used in your list.

ListView always on top

I use below code to update listview.
setListAdapter(MyAdapter);
MyAdapter.notifyDataSetChanged();
getListView().setEmptyView(empty);
But each time update, the list always show top one.
I want to let it only update data, but not focus on top.
How to do it?
Use a ArrayAdapter backed by an ArrayList. To change the data, just update the data in the list and call
MyAdapter.notifyDataSetChanged().
Although you can call notifyDataSetChanged() on SimpleAdapter too.
You need to add listview.setSelection(your_collection.size());. This will make the last item in the ListView as selected when you refresh the ListView. Also call this after refereshing the ListView.

Categories

Resources