0

我有一个带有 ListView 的布局。在 onCreate() 中,我将 Adapter 设置为 ListView,以显示列表,这很好。

但我尝试像这样访问列表视图的 ItemView。

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

我需要访问wantedView,有人可以帮助我吗?

LogCat 说:

--"无法查看

- 真的

谢谢

4

1 回答 1

0

将您的代码以 int WantPosition = 1 及以下开头移动到您在设置适配器后发布的 Runnable:listview.post(runnable)

于 2013-06-11T13:04:18.040 回答