Android: delete item from ListView - android

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.

Related

SimpleCursorAdapter.bindView() getting called for first 4 items many times when changing layout of listitem

I have a ListView and a SimpleCursorAdapter subclass that display data from an SQLite database . I have customized my adapter in such a way that when I click a listitem it expands/collapses to show more details. Everything is working fine, but I discovered a weird behaviour that's using unnecessary processing.
Let's say I have 200 items in my list, all of which are in a collapsed state (this just means that some of the TextViews inside them have visibility="gone"). Now I scroll down the list to item number 100. bindView() gets called for each item, with _id=1, _id=2, ... , _id=99, _id=100 as expected. Now I click item 100 and expand it (change some of it's TextView's to visibility="visible"). One would expect bindView() to get called for _id=100 and perhaps some of the surrounding items like _id=98, _id=99, _id=101, _id=102 because the ListView needs to be redrawn. But here's the weird part - bindView() gets called for _id=1, _id=2, _id=3, _id=4 (which are of course way off the screen). And that's not all - it does this twice, so we've got 8 bindView() calls for the first 4 items in the ListView, which are not anywhere close to being displayed on the screen. The surrounding items don't get a bindView() call, not even item number 100 gets one!
Since everything is working as it should I can live with this, but I'm really curious a as to why this strange behaviour is going on. Anyone have any idea?

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

Android Listview jumps on change in adapter content

I am using a scroll listener to detect the end of the Listview.The list initially has 20 items. When the end is reached I populate the list with 10 more items from the DB. At the same time I remove the top 10 items which aren't visible. Since I delete the top 10 items the newly added items are shown on screen and the scroll listener detects end of list and populates 10 more items. So my two problems are
1) How do I stop the position of the item I am currently viewing from changing ?
2) How to prevent the onScrollListener from being called multiple times ?
I guess if you could help me with the first problem, the second would automatically be taken care of.
By the way ,I call notifyDatasetchanged to update the content of the adapter. Kindly help, and thanks.

Android recreating ListView

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

Android - ListView event when user reaches 50º list item

I want to set a simple listener in a ListView so when the user passes through the 50º item of the list, I call a thread that will append 100 more items into that list.
I am looking for an event to do that for over an hour now and I don't seem to be able to find one. There are tons of events for when a list item will be clicked or something like that, but I can't find one for when the user simples passes through the item in the scroll processing.
Thank you so much in advance!
Put the ScrollListener on list view, when it reaches end of the list u will get the last item, then add the progressbar at the footer and then start thread where u can load the other items, and notifyDataSetChanged() for adapter , and you will get the list with added items..!!
And you can check whether the item number 50 is reached, in onScroll().
I think there is no need to do this, because the ListView recycles the item views. So you dont occupy memory for the listview items (per each value), you just use memory for the ArrayList(or any data type you use for feeding).
Also you can do this whenever user reaches the end of the listview (last listview item) then refeed the adapter with the new value and then call .notifyDataSetChanged().
About reaching 50% of listview visible items can make things scrambled because you'll always have the 50% reached, otherwise you can improvise (reaching 50% on the ArrayList instead ListView) by checking if more than 50% of the existing ArrayList is displayed, let's say you 10 items and the 6th item is on the listview count / 2, you check if the on that half listview item is more than 50% of data feed for the listview you feed with new data. (I might be unclear)
I hope the idea will help you.

Categories

Resources