我正在创建一个 ListView ,其中有两个 TextView
和一个自定义 View 作为选择指示器,用于加载我正在使用的数据 CursorAdapter
并压倒一切bindView
和newView
。但问题在于保留选择索引。
当我点击列表项时,我将其位置保存到 View 中,如下所示
View v1=(View)view.findViewById(R.id.viewColor);
v1.setTag(position)
但在 bindView
内我没有获得可以通过从 View 标签获取位置来执行匹配的位置,如下所示
Integer position = (Integer) v1.getTag();
但我没有机会比较我们进入getView
的方式。我尝试了 cur.getPosition() 是光标中记录的位置,它与 v1.getTag() 不匹配。
我尝试按如下方式覆盖 getView,但没有获得准确的位置
@Override
public View getView(final int position, View convertView, ViewGroup parent){
this.position = position;
return super.getView(position, convertView, parent);
}
那么我怎样才能获得bindView中的位置?我需要使用 getView 还是什么?我探索了很多线程,但是当涉及到bindView时我没有得到任何明确的答案。
编辑
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long arg3) {
view.setTag(position);
// i save the position on list item click so that when adapter loads the
// the list i can match the position with the current one
}
最佳答案
为此,您首先需要重写 getView()
并在此处设置位置,稍后在 bindview()
中访问它。
@Override
public View getView(int position, View convertview, ViewGroup arg2) {
if (convbertview == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertview = inflater.inflate(R.layout.your layout,
null);
}
convertview.setTag(position);
return super.getView(position, convbertview, arg2);
}
并在bindView()
中获取您的位置,例如
@Override
public void bindView(View view, Context context, Cursor cursor) {
int position=(Integer) view.getTag();//here is the position
}
关于android - 使用 CursorAdapter 获取 bindView 中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27666203/