Android - ListView event when user reaches 50º list item - android

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.

Related

How to show listview items and limit them to have pages like?

I'm using Android Studio, and I have a listView that musts display an important amount of items. showing all of these items have a huge impact on performances. SO I would like to show them 10 by 10, and with a button show the 10 next items. After some researches, I found this android How to limit list items display in ListView and a button show more and this How to limit list items display in ListView by 10 and next 10after clicking next button. But these didn't lead me to a success. The 1st link looks easier but I didn't know where to put the code samples to make it work. Thanks for help !
You dont need to show "Load More" Button always. You can use Android's Recycler View for this. It will load only the data which can be shown on screen. Rest of the data will be loaded as you scroll down. And the view which is scrolled up (Vanishing views) are recycled automatically.
Check this links
https://developer.android.com/training/material/lists-cards.html
https://guides.codepath.com/android/using-the-recyclerview
https://www.binpress.com/tutorial/android-l-recyclerview-and-cardview-tutorial/156
First link question have a simple answer, If you made a custom adapter for populating listview then there will be a getCount() method which will return the number of items you want to show on listview. if you have not getCount() method then simply override it.
Link
there i(variable) have that value which number if items you want to show on listview, on refresh button click increment the value of i(variable) that will again refresh the whole listview and add the total number of items in listview which is the value of i(variable)

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

Get all data of one row by click on a Button?

I use below code for get data of one row
View vListSortOrder;
vListSortOrder=getListView().getChildAt(MyPositionOfRow);
TextView edit=(TextView)vListSortOrder.findViewById(R.id.MyLabelId);
Toast.makeText(getApplicationContext(), edit.getText(), 1).show();
But My problem is item that not shown in the listView
for Ex: I have 20 item .In MyPhone Screen 10 item of ListView displays.
when MyPositionOfRow set to 0_9 not problem.
when MyPositionOfRow set to 10 or above my program will be crash
How fix it?
Or other way to get data of one row in listView.
You have to use Adapter for interaction with ListView
Your application is crashing, because during UI execution in memory existed only 9 items of ListView.
If you will scroll list to the up - upper item moves to the bottom for memory reducing. And we have to use adapter for correct changing of the item's content.
Really nice article you can find here:
http://www.vogella.com/articles/AndroidListView/article.html

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.

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.

Categories

Resources