Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all ListView items(rows)

How may I get all children of an item of ListView ?
There are methods to get child at position but I was not able to find any method which returns collection of all children in an item of ListView.

Update : I just found out that I need ListView items(rows) instead. How may I achieve this ?
What I am doing is, comparing the selected ListView item with all items.

like image 426
Nitish Avatar asked Sep 13 '14 09:09

Nitish


4 Answers

You get all list item from list adapter through iteration as below

for (int i=0;i<adapter.getCount();i++){
     adapter.getItem(i);
}

Update : You can compare specific item using index with list adapter all items as below :

for (int i=0;i<adapter.getCount();i++){
    if(adapter.get(selectedIndex)==adapter.getItem(i)){
       // TODO : write code here item match 
       break; // after match is good practice to break loop instead compare rest of item even match
    }
}
like image 185
Haresh Chhelana Avatar answered Nov 15 '22 13:11

Haresh Chhelana


Use this method to get all insight and out-sight view of list view:

for ( int i = 0 ; i < listView.getCount() ; i++){
    View v = getViewByPosition(i,listView);
}

public View getViewByPosition(int position, ListView listView) {
    final int firstListItemPosition = listView.getFirstVisiblePosition();
    final int lastListItemPosition =firstListItemPosition + listView.getChildCount() - 1;

    if (position < firstListItemPosition || position > lastListItemPosition ) {
        return listView.getAdapter().getView(position, listView.getChildAt(position), listView);
    } else {
        final int childIndex = position - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }
}
like image 28
Jigar Avatar answered Nov 15 '22 13:11

Jigar


I guess if your items in the ListView are of type ViewGroup you could do the following

ArrayList<View> children = new ArrayList<View>();
for (int i = item.getChildCount() - 1 ; i>=0; i--) {
    children.add(item.getChildAt(i));
}
like image 40
hoomi Avatar answered Nov 15 '22 12:11

hoomi


May be you are looking for something like this. You need to have access to the rootView from which you can get the child views. onItemSelected is only used as it gives the rootview of clicked position.

public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
ImageView imgView = view.findViewById(R.id.myImageView);//your imageview that is inflated inside getView() of adapter
//similarly for other views 
}
like image 1
Illegal Argument Avatar answered Nov 15 '22 12:11

Illegal Argument