How to get all children (visible and invisible) from a ListView?
Asked Answered
F

5

7

My problem is similar to ListView getChildAt returning null for visible children, but despite searching I cannot find a solution.

I have a ListView with a Scroll. The ListView has 10 items, 7 of which are visible and 3 are hidden by scroll. I also have an external method (out of adapter) that must get all of the children from this ListView (e.g. using getChildAt()).

I need all 10 of the items, but the last 3 are null objects. I've tried code like the following:

getListView().smoothScrollToPosition();

But this doesn't work.

I think that I don't need to post the rest of my code, as the description says everything?

Franchot answered 19/12, 2012 at 10:55 Comment(5)
But this doesn't work. - and it's normal that it doesn't work because the ListView contains only the visible rows, it doesn't create the views for the invisible children. Why do you need access to all of ListView's child Views?Chez
Each row contains a RadioGroup and a Button that must be fill by user. All rows are required.Franchot
You're approaching this the wrong way. The user's action should be reflected in the adapter's data and from there you should get the status of the RadioGroup.Chez
That's right. I will try it now. Thanks Luksprog.Franchot
Hi there, about getting all data it's ok (External Method). No nullable data. But the ListView doesn't keep the current values from RadioGroup in running time. Ex: Line 1: O O * O Line 2: O O * O .... Line 10: O O * O When i roll up with scroll: Line 1: * O O O (Back to Original Value) I know, this is about the ListView that only keep the visible rows at each position, but i could't find any solution. Any suggestions? Thanks a lot for patience and support.Franchot
C
4

As you have already seen you can't get all the child row views from a ListView simply because a ListView holds only the views for the visible rows(plus some recycled rows but you can't reach those). The correct way to do what you want is to store whatever data in the adapter's data and retrieve it from there.

But the ListView doesn't keep the current values from RadioGroup in running time.

I've seen that you have some problems with this so I've adapted some old code to build a basic example, code that you can find here.

Chez answered 19/12, 2012 at 21:3 Comment(2)
Sounds nice. I will try you help. Thanks.Franchot
you can get all Visible and invisible items from List View. Please see https://mcmap.net/q/1075842/-get-all-listview-items-rows/…Spiritism
H
2

I don't think so you need to add scroll view for a listView. Scroll automatically works on ListView. Try your application without adding scroll view and I'm sure it'll work as you needed.

Humberto answered 19/12, 2012 at 11:2 Comment(2)
Hi Rohit,actually it have an automatic scroll as you said. I dont implemented it.Franchot
can you post the code of your adapter. According to me there is some problem within your adapter class.Humberto
A
1

The reason those children are null it's because they really do not exist and they will never exist, if only 7 children are on the screen at one time, the system will only create 7 and re-use by passing the convertView back to the adapter getView() method.

If you want to grab information regarding your whole dataset you should search on the dataset itself, instead of the views on the screen. E.g. if it's an ArrayAdapter, loop the array; if it's a CursorAdapter, loop the cursor; etc.

Arnuad answered 19/12, 2012 at 11:9 Comment(1)
I will try it. Let's see. Thanks Budius.Franchot
M
1

The non-visible children of a listView don't actually exist. When they become visible, one of the redundant views is recycled or a new view is generated. So you can't actually access all the views. Why do you want to? Whatever changes you want to make should be made to the data that populates the views rather than the views themselves.

Matronize answered 19/12, 2012 at 11:13 Comment(3)
User will fill all the rows, and he will use the scroll to fill the last ones. That's the problem.Franchot
Whenever a row is filled, use the appropriate listener to record the value in a data element in the adapter. You can then use this data however you want. When getView() is called with that position again, you can restore the state using this data.Matronize
I've tried it before, but the position don't keep on a pattern, always in random.Franchot
W
0

There are a few point that you need to take care of: 1. List view provides inbuilt scroll functionality, So don't use Scroll view. It will only mess up things. 2. List view doesn't contain ALL the children. When you scroll it, it creates only visible items on run time. 3. If you want to get all the children altogether, Better keep an ArrayList of the child objects that your list has. You can add or remove children to this ArrayList as per requirement.

Ware answered 23/7, 2014 at 10:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.