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?
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..
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.
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);
}
}
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With