0

I have in a layout with a ListView. In onCreate() I set Adapter to the ListView, to show the list, this work fine.

But I try to get access to a ItemView of the listview like this.

https://stackoverflow.com/a/2679284/1149815

public class MainActivity extends Activity {

private ListView listView;  

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView)findViewById(R.id.listView1); 
    listView.setAdapter(new TestAdapter(this, getLayoutInflater()));

    int wantedPosition = 1; 
    int firstPosition = listView.getFirstVisiblePosition() - listView.getHeaderViewsCount(); // This is the same as child #0
    int wantedChild = wantedPosition - firstPosition;

    if (wantedChild < 0 || wantedChild >= listView.getChildCount()) {
      Log.w("--", "Unable to get view");
    }

    View wantedView = listView.getChildAt(wantedChild);

    Log.i("--","" +( wantedView == null) + "");
}}

I need access to wantedView, can someone help me?

LogCat says:

--"Unable to get view

-- true

Thanx

Community
  • 1
  • 1
Bobert
  • 226
  • 4
  • 16

1 Answers1

0

move your code starting with int wantedPosition = 1 and below to the Runnable that you post after setting the adapter: listview.post(runnable)

pskink
  • 23,874
  • 6
  • 66
  • 77
  • leave starting code in onCreate up to listView.setAdapter(), the rest of the code put in some Runnable and after listView.setAdapter() call listview.post(runnable) – pskink Jun 11 '13 at 19:53
  • Perhaps you mean this: http://stackoverflow.com/a/7304055/1149815 Thanx... I will try it – Bobert Jun 13 '13 at 07:20