2010-10-21 40 views
0

所有,Android LIst视图

首先,我想知道什么是列表视图项的索引?就像数组的索引(a [0],a [1] ....)。

其实我想得到列表项的索引。它始终显示我-1 这里是代码。

protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 

     Object index = getListAdapter().getItem(position); 

     Intent myIntent = new Intent(this, newsdisplay.class); 
     //Create the view using FirstGroup's LocalActivityManager 
     View view = newsgroup.group.getLocalActivityManager() 
      .startActivity("show_city", myIntent 
      .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) 
      .getDecorView(); 

     //Again, replace the view 
     newsgroup.group.replaceView(view); 
} 

请帮助我如何获取列表视图项目的索引,并且这是独一无二的其他项目?

回答

1

有两个不同的数字与listView中的每个项目相关联。

position是该项目在arrayAdapter中的位置。

index是项目在可见ListView项目中的位置。

因此,对于任何给定的项目,position应该随时间保持不变,但index将随着不同项目的可见而改变。

目前尚不清楚你正在尝试与您的代码段的事,但你叫index的对象应该是在你的ArrayAdapter内position的对象,所以a[position]

如果一个项目的index是-1,我会认为这将意味着它目前不在屏幕上可见。