[英]Android - How to get ListView item at absolute position?
So ListView Adapter has the getView method and it's called everytime the display of the listview is changed... so the 0th element becomes the 1st element in the viewport. 因此,ListView Adapter具有getView方法,并且每次更改列表视图的显示时都会调用它...因此,第0个元素成为视口中的第一个元素。 I think... 我认为...
How do i basically access the 0TH element in the Listview? 我如何基本上访问Listview中的0TH元素? Not the 1st element in the viewport? 不是视口中的第一个元素? First element in the entire ListView? 整个ListView中的第一个元素? I want tot access elements by an absolute index so it doesn't matter which one is visible, i want to access them based on their index in the ListView. 我想按绝对索引访问元素,所以无论哪个可见,我都希望基于ListView中的元素访问它们。
How do i do this? 我该怎么做呢?
You can't access any View
in the ListView
by index, because it's recycling them and essentially there are no views other than you see plus some cache. 您无法按索引访问ListView
的任何View
,因为它正在回收它们,并且除了您看到的视图和一些缓存外,基本上没有其他视图。
And actually you shouldn't want to do this. 实际上,您不应该这样做。 If you want to manipulate with the ListView
's data, change the underlying collection via Adapter
, so in overriden getView()
of your adapter you can decide how to render the particular view (what background color to set in your case). 如果要处理ListView
的数据,请通过Adapter
更改基础集合,因此在Adapter
重写getView()
,您可以决定如何呈现特定视图(在您的情况下要设置的背景颜色)。
So you'll have something like this: 因此,您将获得以下内容:
private class Item {
Color getBgColor {...}
}
private class MyAdapter extends BaseAdapter { // or WhateverAdapter you have
Item getItem(int id) {...}
public View getView(int position, View convertView, ViewGroup parent) {
...
convertView.setBackgroundColor(getItem(position).getBgColor());
return convertView;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.