我有一个带有ListView的布局。在onCreate()中我将Adapter设置为ListView,以显示列表,这工作正常。
但我尝试像这样访问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说:
- &#34;无法获得视图
- 是的
感谢名单
答案 0 :(得分:0)
将以int wantedPosition = 1及更低版本开头的代码移动到设置适配器后发布的Runnable:listview.post(runnable)