Checking items in a ListView by Default - java

I have a ListView (actually a ListFragment, but I believe they are functionally essentially the same) that is backed by a SimpleCursorAdapter. When the fragment is loaded, it is passed a long[] of ids that represent the items to be checked by default. So when the data has finished loading from the database, I need to "check" the items in the view that have are contained in the list of ids.
How would I do this? It seems like such a simple task, and I can figure out the logic necessary to perform the actual "checking". But I can't figure out where to place the logic so that it will work properly. I tried putting it in the onViewCreated(View, Bundle) method, but the View passed in is not a ListView and I don't know how to iterate though the views returned by getListView().

Related

Android How many items can a list fit while scrolling with RecyclerView

I have a simple recyclerView. When scrolling, items are getting fetched from a Room Database and being added to the recycler's list.
I wondering how many items can be added before throws OOM?
Is there any way to control this?
Example: In instagram's search section you cant scroll down forever right?
RecyclerView creates limited number of views, then populate them by your data. So, there is no limits to show the items until some problems happened like reaching the Integer MAX_VALUE (because of containing list position), etc...

can you put an expandable list within an expandable list

is it possible to make an expandable list within an expandable list
i am making a recipe application and i've three expandable lists of the same type - vitamins, minerals and macro-nutrients created with expandablelistview with android studio
is it possible to put these three expandable lists under an expandable list called nutrition and if so could you briefly outline how please. thanks in advance
Short answer, yeah you can but you really really shouldn't.
I've tried this before and it didn't end well, I can't remember the exact reasons but conflicts arise between touch listeners and the measure passes when trying to layout the views inside the different adapters don't calculate correctly.
You can find a number of custom ExpandableLists trying to do what you want around the web but I found they also had similar issues.
What I ended up doing was building my own expandable listview. If you check the source code for Android's ExpandableListView and ExpandableListAdapter you'll find that it's basically
just a regular listview
a list of items that are visible
an Async filter for the data source
some meta data to tell if its a child or group, expanded or collapsed, etc
So if all your item are collapsed the visible list only contains the Group Items.
Expand a Group Item and the async filter gets triggered and populates the visible list with the Group Items and the Children of the expanded Group Item.
Depending on the meta data it will call either getGroupView or getChildView.
So if you want you can take a shot at writing your own.
I wrote a blog post for an ExpandableList that I wrote that does, in theory, infinite levels of expansion. NLevelExpandableListView

Android: 1 Adapter to multiple Array Lists

I have a base adapter and 5 array lists which contain different data.
Based on a selection, the contents of the array list are populated on the list view.
At the current moment this functions correctly, however each time, a new selection is made, an entire new adapter is being made and set. Would like to know if there is an alternative way of changing the data displayed without having to set a new adapter.,
I am fully aware of notifyDataSetChanged()
If you are using an ArrayAdapter (just a guess), then there are functions to change the used list (http://developer.android.com/reference/android/widget/ArrayAdapter.html):
clear()
addAll(Collection<t> c)

Android: How to clear a list view without removing data from the array list?

I am attempting to create a list view which displays different data based on a selection from an a spinner.
My list view is implemented via a base adapter which works fine.
My data is stored in an array list of objects which each contains fields that store individual data and based on the selection from the spinner filters the data stored in the array list and displays it on the list view.
But my question is how can I clear a list view content without clearing its underlying data (stored in the array list) and calling notifyDataSetChanged? I want to be able to preserve the data stored so if the user goes back to a previous selection, the data is still present.
Thank you for your time in reading this.
As your data is stored into an ArrayList, you can simply use:
listView.setAdapter(null);
It will remove the adapter of your ListView, and so, remove all the items displayed.
The data will still be in your ArrayList so if you want to re-display this ListView, you just have to recreate your Adapter and set it again. You can even create a global instance of your BaseAdapter so you only have to create it once.
You could just pass another ArrayList to the adapter with the new data to show inside the ListView.
Or, you could create two ArrayList inside the Adapter, one named original which contains the original elements of the ListView and another currentElements which will be the real ArrayList to show changed as you want. With some custom methods, you can choose to switch between the two versions of the ArrayList.
It's could be the same concept of an Adapter which implement a Filter.
Anyway if i understand your problem, you want to change the content of a ListView based on the selection. Well, in this case i would send another ArrayList to show.
Create a list of adapters, one adapter for each selection. Then simply switch to a different adapter when the selection is changed with listView.setAdapter(adapter)

Multiple lists in one ListView

I have a series of buttons that I would like the user to press in order to show a different list in the list view. Say, button one brings up list one. User adds a few items to list one and then presses button two to open list two; user then adds a few items to that list.
How would I go about having multiple list that share a single list view? Also, how would I have the data of one list be saved so when the user switches lists... they can switch back to the original and have their items still presented in the list?
Thank you!
I guess what you are looking for is ExpandableListView
here is an example too
You could have multiple lists stored in some type of array. A list could be used for each list, and then when you are setting the listview's adapter, pass the current list object. Of course, after each item is added during the adding process(when user is adding items) you would need to add the items to the list, and then reset the listview. And as long as you kept the lists in context the entire time they would not be GarbageCollected. Once the user is done with the lists, you would need to save the lists somehow, unless this is just a temporary context where everytime you revisit the page it shows the default value for the lists.

Categories

Resources