Get all ListView items(rows)

前端 未结 4 1177
孤街浪徒
孤街浪徒 2020-12-17 22:53

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 collectio

相关标签:
4条回答
  • 2020-12-17 23:03

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-17 23:22

    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));
    }
    
    0 讨论(0)
  • 2020-12-17 23:26

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-17 23:26

    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 
    }
    
    0 讨论(0)
提交回复
热议问题