Unable to update the listView in Android - android

I have a ListView which displays 5 listitems. When the user selects the last item (5th item), a hit goes to server and the next set of 5 items is displayed.
I have implemented the code for displaying the first 5 items. I am also able to fetch the next set of items from the server, but the items are not updated in the ListView.
If required I can share the sample code.
Kindly provide hints/sample source code.

Did you try clearing the list items first and then update the list elements?
In order to commit the updates you should call notifyDataSetChanged() ..

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)

Load list items on demand

I would like to load a list of messages on demand.
I show 20 messages on my listview and when the user scrolls on top of list there is a button to load more 20 messages.
Like whatsapp, when user clicks on the button, more messages are loaded above and the user can scroll 20 more messages.
I have tried to make it with Loaders, every time the button is clicked I publish an event that load messages on the adapter.
However when the adapter is releoaded with new itens the focus goes to end of list.
I have tried to set the focus to the right positon, but the Loader's events reload the adapter and the focus come back to end of list.
The only solution I found was to set the focus after a time, but the user can see the list going to end and coming back to the right postion.
How can I load messages on demand like Whats app?
You have to add only 20 objects in your ArrayList and add HeaderView in your list adapter .On click on headerView .It will load more 20 items in your list adapter . and call notifyDataSetChange(); afterwards.

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.

android customize list view prepends results

I am using list view using "ArrayAdapter" to display number of items while user is looking at results when user click on load button i want results to load and prepends(add in the beginning)
Found solution
use adapter's insert method and add it to position 0 every time and also before adding reverse order so that first item will display at the beginning if don't reverse first item will display at end.

Categories

Resources