Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - listview get item view by position

I have listview with custom adapter (base adapter). I want to get view from listview by position. I tried mListView.getChildAt(position) , but it is not working. How can i get item view by position?

like image 375
alashow Avatar asked Jul 17 '14 19:07

alashow


People also ask

How to get view by position in listView Android?

get view like this listView. getChildAt(pos - listView . getFirstVisiblePosition()); <position is the position on which you have clicked) change the view and then call refreshDrawableState() on that view to update it..

How pass data from listView to another activity in Android?

Implement ListView 's OnItemClickListener, once you handle this event, try to get the location of the row that was clicked. Once you get it, access that particular row position in the source array (or whatever else you're having). This way, you'll have the data that you want to pass to another activity.


2 Answers

Use this :

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

    if (pos < firstListItemPosition || pos > lastListItemPosition ) {
        return listView.getAdapter().getView(pos, null, listView);
    } else {
        final int childIndex = pos - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }
}
like image 158
VVB Avatar answered Oct 04 '22 08:10

VVB


You can get only visible View from ListView because row views in ListView are reuseable. If you use mListView.getChildAt(0) you get first visible view. This view is associated with item from adapter at position mListView.getFirstVisiblePosition().

like image 30
Matt Twig Avatar answered Oct 04 '22 08:10

Matt Twig