Android - Filtering a listview - android

I want to implement the following:
Listview 1 contains items with checkboxes. If one or more items are checked, listview 2 should only show items that contain the checked items.
What would be the best way to accomplish that?

I had a similar question about how to implement checkboxes in each listview element. It could be tricky since the adapter refreshes the states of each checkbox on scrolling. Check David Scott's answer and my comments on it for proper use. Also check out Joey's answer after that.
ListView adapter with many checkboxes
Regarding your 2nd listview you will have to check your boolean array which rows are checked. Then get all elements at those indexes from your collection and save them. Either you:
Use all the element rows that was checked(in some container class) and set these to your listview adapter number 1. Then use adapter.notifySetDataChanged() and these will appear.
If you want to keep your listview number 1, create a new activity with the 2nd listview, pass the chosen objects to it and use them as elements. Or simply put a listview below the 2nd one with the checked rows.
Dont know how much you know about listviews but this are my 50 cents about the topic.

Related

Should I use 2 ListViews or 2 Adapters with 1 ListView

When it comes to a screen that contains 2 different lists depending on the state of say a checkbox at the top, is it better to use 2 listviews that you hide/show when the checkbox is selected or should I have 2 different adapters and attach/detach each from a single listview?
Are there any benefits/downsides to either of these solutions?
You should go with 2 adapters and 1 ListView. the reason is simple - either way you are going to have 2 Adapters (as per your question). Having one ListView would make the code simpler and there will only be a single instance in your xml file. Depending upon the state of the checkbox, you just need to change the adapter the listview points to and notify of this change.
If you had two list views, you would have to hide one. Just because the ListView is hidden doesn't mean that Android does not have to bother about it. You just have an object (size will depend) consuming resources sitting in the background.
Both of the proposals are wrong.
If we assume that you have same Object type to be listed in a ListView (Let's say a user object with name and id fields), then you need to have one ListView and one Adapter. When user wants to switch the data, what you need to do is to send the new data set to your adapter and then refresh it by calling notifyDataSetChanged(); method of adapter.

Endless Adapter with custom listview adapter in android

I am using this link to create custom listview in my app.
First I want to display listview with 5 items. As I scroll the list it should parse data and load listview for next 5 items.
Listview row contains 5 textviews.
How can I achieve this?
Please help me.
Thanks in advance
I want to display 5 items in single row
That has nothing to do with EndlessAdapter. That has everything to do with the ListAdapter you are putting into the EndlessAdapter. Start by not using EndlessAdapter and getting your list looking the way you want. Then, and only then, add in EndlessAdapter to add more data to the list as the user scrolls.
There are countless examples online of how to create rows in a custom ListAdapter that have more than one thing in them. Here is a free excerpt from my book on how to subclass ArrayAdapter, override getView(), and handle more than one widget in a row. You specifically will want to look at the "Customizing the Adapter" section.

Android: How do I set TextView for each item in a ListView?

I need someone to point me in the right direction on this....
In my Android application I have a ListView with custom views for each row containing various textviews each. I retrieve weather information for multiple locations and then want to write the results for each location to its own listview item. The question is, how do I refer to each textview on each listview item?
I can't do it by name using findViewById, can I? If there are three items in the list, will the temperature textview for each item be names the same, just differ by an array index? If so, how do I specify TextViewName(0), TextViewName(1) and TextViewName (2) for the three list items?
Yes, I'm still very new to Android....but loving it!
You need to do this when each item is created (or recycled) in the getView method that you should override in the adapter.
Watch the google io talk from 2011 on list views and it should be quite clear how they work.
http://www.youtube.com/watch?v=wDBM6wVEO70
Use a type of adapter:
http://www.java-samples.com/showtutorial.php?tutorialid=1516

Android dynamically calculate number of columns

I have a dynamically created list of items which should be displayed in either two or one column depending on the space the containing text needs e.g. if the text is long it will only be one item per row, otherwise two. Obviously they should all be the same size (half the screen size).
As far as I know there's no standard Android view with Adapter that does that. With a GridView you can have multiple columns, but not some rows one column and others two. A TableLayout could stretch views, but also here you have to know how many columns you need per row. Plus it doesn't have an Adapter.
So what I want to know a) is there any control that I'm missing that supports something like this or b) what would be the easiest solution for this problem?
EDIT: the items also contain a CheckBox and I need to keep track of the checked state so I can't just put two items in one view using an Adapter.
I'd say that this is a hard problem to solve using standard components, due to the problem of mapping data to items to rows.
If you for example use a Cursor with x rows to feed the adapter with data, then the total item count as seen from the Adapter is also x. However, since you're conditionally mapping two items to the same row, it means that a ListView will see y rows in the Adapter, where y <= x. But you cannot easily tell from the beginning what y will be. Furthermore, if the ListView asks the Adapter for item i where 0 <= i < y, there would be no (easy) way for the Adapter to determine which elements from the Cursor that i would map to.
That being said, a viable solution would be to subclass AdapterView or ListView and implement the layout of the elements yourself. As you're getting each item from the Adapter, you'll measure and layout it, depending on the sizes of the surrounding adapter items.
A different solution that could work for you if you don't have a large number of elements is to use two custom Adapters, one called ItemAdapter and one called RowAdapter. The ItemAdapter will inflate the actual items based on (the presumed) Cursor. The RowAdapter will use the ItemAdapter to get the items and merge them into rows. The ListView will in turn use the RowAdapter. The issue is that to know how many rows the RowAdapter will produce, it is necessary to measure all the items from ItemAdapter before the RowAdapter is connected to the ListView.
As far as I know there is no ready solution for your problem. I haven't yet tried something like this, but I would use a LinearLayout as the list item. Then just create your own Deflater (e.g. CursorAdapter) that deflates the Layout and checks the length. If needed you should be able to add a new View (e.g. TextView) to the LinearLayout.

Android - ListView with only 1 expandable item

I have a listview with tickboxes, which uses an Adapter and which works well.
And I know how to make a whole list expandable.
But, it it possible to have just a single item expandable, or does it have to be the whole list?? If so, how is it done?
You would need to use Lists of one item each. Unfortunately there is no SDK widget that handles the special case you need.
You can insert (or remove) the 'child' items in the right place within your adapter's data set when the 'parent' is clicked using a normal ListView.
You could actually use an ExpandableListView and think of the non expandable items as group item with no child.
That's the way I have done it. I have my own adapter which handles showing/hiding the indicator depending on whether the group item has children or not.

Categories

Resources