Single ListView Adapter to serve different data to single ListView - android

I have my own custom ListView adapter which gets say for example 100 records. Now, out of these 100 records I want to show only select records in ListView. These select records could be in random order.
I have four such buttons and every time I click button I want to populate different records from adapter without making a new request again.
Any idea how can I achieve this?

Make two lists inside your custom adapter.
One - with all your records. And one - with records, that you want to display.
On click, you fill second list with data from first list somehow, call notifyDataSetChanged(). On other click, clear that list, fill with another records, and call notifyDataSetChanged() again.

Related

How to make ListView loop?

I have created listView with about 20 items.
While navigating through keyboard when it's on last and when user hits down button I'd like it to go to first item.
Just like keyNavigationWraps : bool in QML.
You should not iterate the list views, instead you should keep all that data (price/quantity) in a list and iterate over that if you really need to do that. That list can be used to populate the ListView as well.

Android - Adding Last item in the end of a list ListView

I am having a custom list view. In general it shows some 5-6 data depending to the data. i want a small message in the end of a ListView saying "End of list of results". How to achieve that?
Use addFooterView method of ListView.

Android: How to dynamically replace a list view with another list view like Instagram with onClick?

I'm building an android app and I'm trying to replace a list view with another list view if you click a button, like the notifications page on Instagram. On that page, if you click on the top "following" button it will show you a listview of what your followers have liked. If you click on the "you" button it will show you a listview of what people have liked your photos.
Any help would be greatly appreciated!
You can do it by following ways,
1. add Two listviews and can change the visibility as per your requirement.
2. On button click you can load the other data into same list view and can update your adapter in the same list view.
in 1 you have to load two list view at first which will consume more time if data is larger sure you can write a login in asynctask to load list views in background thread.
in 2 you have to update your adapter at the button so you will have to provide some progress bar of dialog for user while you list view is getting update.
You can use either of this whichever suites you best.
Simply , You don't need to switch Listview , you only need to switch adapters .
eg, you can switch to mFollowingAdapter when clicked on Following button and switch to mYouAdapter when you select "You" tab. that's it.
You should write a list, that has a custom adapter. This adapter will be able to display BOTH views you want to display.
If the data to be displayed is the same format (ie. both have an imageview next to a textview), you are in good shape.
When you want to switch to a different list, get the information you would like to display, replace the data in your the collection backing your list, then notify the list that the data has changed, and it should redraw.
So, this might look like:
create ArrayList() with data A
setup List, with this data and display
replace the ArrayList() with data B
call listView.notifyDataSetChanged
You can still do this if the Data A, and Data B have different views, in this case, you would need to handle this logic in your custom adapter.

Load more messages in listview implementation in android

I display first 20 records fetched from server in a listview.
I have baseadapter which is binded to listview as an adapter, and i pass records fetched from server to this baseadapter.
I set a footer item as textview attached to listview, onclicklistener of same, it fetches new 20 records from server, then i add those 20 records and pass to baseadapter.
But it reloads whole listview again, and displays all 40 records but starting from list item 1.
I want to display whole list but cursor point should be from new items added in list, as similar to "Email application" in android.
In other words, only new items should be refreshed to
For that you need to call the below code when you notify your adapter.
listview.setSelectionFromTop();
It set your item from the top.
Your app can keep current listview position and after updting listview you can navigate to this position through setPosition method

How to add data in list below previous one?

I want to add data in list. When I click the button, it takes data from different classes and adds it as a row in the list and comes back.
And again, when I insert data in form and press the button, it adds it below the previous one on another row. So an array is created of rows, and now when I click on any position of list item row, I get its content. So how to implement it?
First thing you need to do is whenever you add new data in the list you should call notifyDataSetChanged(). For getting its content you need to write onItemClickListener() and in listener you can get the content through its position id.
Your question is not very clear. But from what I understand, you what to use a ListView or a ListActivity.
In either case, you will need an adapter which manages the data displayed in the list. I would suggest that your "different class" which provides the data should subclass BaseAdapter.
You can then bind the class providing the data to the view using setAdapter(ListAdapter)
When your data changes, all you need to do is call notifyDataSetChanged() on your data class which provides the data and the view will be updated.
To get the clicked item, you will need to create and set an OnItemClickListener on your ListView
See this list activity example for guidance.
deep copy the generated list and append it to the previous array

Categories

Resources