How to get all children (visible and invisible) from ListView?

My problem is similar to ListView getChildAt returns null for visible children , but despite the search, I can not find a solution.

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

I need all 10 items, but the last 3 are null objects. I tried the code as follows:

 getListView().smoothScrollToPosition(); 

But that will not work.

I think I don’t need to publish the rest of my code, as the description says it all?

+7
source share
5 answers

As you have already seen, you cannot get all views of child rows from a ListView simply because the ListView contains only views for visible rows (plus some recycled rows, but you cannot reach them). The right way to do what you want is store any data in the adapter data and retrieve it from there.

But the ListView does not save the current values ​​from RadioGroup at runtime.

I saw that you have some problems with this, so I adapted some old code to create a basic example, the code you can find here .

+4
source

I don't think you need to add a scroll view for listView. Scrolling automatically works in ListView. Try the application without adding a scroll, and I'm sure that it will work the way you need.

+2
source

Invisible children of listView do not actually exist. When they become visible, one of the redundant species is recycled or a new view is created. Therefore, you cannot access all views. Why do you want? No matter what changes you want to make, data must be made that fills the views, not the views themselves.

+1
source

The reason these children are null is because they really don't exist, and they will never exist if there are only 7 children on the screen at a time, the system will only create 7 and reuse it by passing convertView back to the getView() adapter .

If you want to get information about your complete dataset, you should look in the dataset itself, not in the views on the screen. For example. if it is an ArrayAdapter, loop the array; if it is a CursorAdapter, loop the cursor; and etc.

0
source

There are a few things you need to take care of: 1. List view provides built-in scroll functions, so do not use scroll view. It will only ruin everything. 2. The list view does not contain ALL children. When you scroll it, it only creates visible elements at runtime. 3. If you want to fully collect all the children, it is better to save the ArrayList of the child objects that are on your list. You can add or remove children to this ArrayList as required.

0
source

All Articles