본문 바로가기
프로그래밍/DB/JAVA/안드로이드/GAE

[안드로이드] ListView의 ROW의 하위 뷰 읽어 오기.

by 아유카와 2014. 11. 18.

   ListView가 10줄이 있다고 가정하고. ListView안의 특정 TextVeiw의 총 합을 구하려면 어떻게 해야 할까?? ListView의 GetCount()를 사용하면 전체 row의 개수를 알 수 있다. getChildCount()를 사용하면 현재 보이는 화면의 +2 된 로우만을 구해 올 수 있다. 쉽게 생각 할 수 있지만 조심해야 한다.  


스택오버플로우에 좋은 예제가 있어 링크한다. 아래 메서드를 만들고 GetCount() 만큼 for문을 돌리면 되지 않을까 싶다. 

http://stackoverflow.com/questions/24811536/android-listview-get-item-view-by-position


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);
}
}