2

This is my code where I want to get a specific child view of a ListView:

ListView list = (ListView) findViewById(R.id.lst);

ArrayAdapter<String> adap = new ArrayAdapter<String>(this,
        R.layout.text, R.id.txt1, data);

list.setAdapter(adap);
int num = list.getCount();
for (int j = 0; j < num; j++) 
{
     view1 = findViewById((int)list.getItemIdAtPosition(j));
     text_view = list.getChildAt(j);
    long lg = list.getItemIdAtPosition(j);
    if (text_view != null) {
        text_view.startAnimation(animation);  //  getting null in text_view .
    }
    if (view1 != null) {
        view1.startAnimation(animation);     //  getting null in view1 .
    }
}
Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
Tushar
  • 23
  • 4

3 Answers3

0

Let say your selected item position is in variable named pos.

  pos=2; // selected item position
  int nFirstPos = listNotification.getFirstVisiblePosition();
        int nWantedPos = pos - nFirstPos;
        if ((nWantedPos >= 0) && (nWantedPos <= listNotification.getChildCount()))
        {
            View view = listNotification.getChildAt(nWantedPos);
            if (view!=null) {

                   //add your code here
                TextView textData=(TextView) view.findViewById(R.id.txt1);
            }

        }
Beena
  • 2,334
  • 24
  • 50
0

The view that you get by using getChildAt is a row contained in your list view. It depends on how you have defined that row. If your row contains a layout and then a textView, the child view that you get is the layout. Then the textView is a child of that layout.

Chus Muñoz
  • 120
  • 8
0

i think you can do that for only visible items, and you should know that all items of listview are not visible at once. Please check the below links, you will solve your problem:

Android: Access child views from a ListView

In an android ListView, how can I iterate/manipulte all the child views, not just the visible ones?

Community
  • 1
  • 1
Androider
  • 3,833
  • 2
  • 14
  • 24