Let's say a list has 4 items, how can I get a view from each menu item of a list by position?
-
1You say list view, but then ask how to get a view from each "menu". Are you talking about a context menu or a list view? – Jack Sep 22 '11 at 15:29
-
Yes, it's not clear exactly what your problem is. A better explanation would lead to better answers. – i_am_jorf Sep 22 '11 at 15:32
2 Answers
Unfortunately the items that are in the ListView are generally only those that are visible. You should iterate on the ListAdapter instead.
For example, in some of my code, I have this:
SimpleCursorAdapter adapter = (SimpleCursorAdapter) this.getListAdapter();
int iNum = adapter.getCount();
for(int i=0; i<iNum; i++)
{
Cursor c = (Cursor) adapter.getItem(i);
// Now you can pull data from the cursor object,
// if that's what you used to create the adapter to start with
}
EDIT:
In response to jeffamaphone's comments, here's something else... if you are trying to work with each UI element then getChildAt
is certainly more appropriate as it returns the View for the sub-item, but in general you can still only work with those that are visible at the time. If that's all you care about, then fine - just make sure you check for null
when the call returns.
If you are trying to implement something like I was - a "Select All / Select None / Invert Selection" type of feature for a list that might exceed the screen, then you are much better off to make the changes in the Adapter, or have an external array (if as in my case, there was nowhere in the adapter to make the chagne), and then call notifyDataSetChanged()
on the List Adapter. For example, my "Invert" feature has code like this:
case R.id.selectInvertLedgerItems:
for(int i=0; i<ItemChecked.length; i++)
{
ItemChecked[i] = !ItemChecked[i];
}
la.notifyDataSetChanged();
RecalculateTotalSelected();
break;
Note that in my case, I am also using a custom ListView sub-item, using adapter.setViewBinder(this);
and a custom setViewValue(...)
function.
Furthermore if I recall correctly, I don't think that the "position" in the list is necessarily the same as the "position" in the adapter... it is again based more on the position in the list. Thus, even though you are wanting the "50th" item on the list, if it is the first visible, getChildAt(50)
won't return what you are expecting. I think you can use ListView.getFirstVisiblePosition()
to account and adjust.

- 14,998
- 7
- 42
- 68
-
hmmm based on jeffamaphone's comments I might have misunderstood the question slightly... it depends on what you are trying to do. note that you can't get "all" subviews because the ListView dynamically manages the views that are on the list... you CAN get to each of the data items that you used to create the view. So if you are trying to do something to the UI for each item, you can use 'getChildAt' but you have to be careful to only work on those that don't return null. If you are trying to do something with the data, then use the solution above. – Michael Bray Sep 22 '11 at 15:35
See here, this question answers the similar problem you mentioned here
In an android ListView, how can I iterate/manipulte all the child views, not just the visible ones?

- 1
- 1

- 3,789
- 2
- 30
- 30