android - 使用 CursorAdapter 获取 bindView 中的位置

标签 android listview android-cursoradapter

我正在创建一个 ListView ,其中有两个 TextView和一个自定义 View 作为选择指示器,用于加载我正在使用的数据 CursorAdapter并压倒一切bindViewnewView 。但问题在于保留选择索引。

当我点击列表项时,我将其位置保存到 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

          }

enter image description here

最佳答案

为此,您首先需要重写 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/

相关文章:

android - 如何在 Mac OS 上安装 ANDROID_SDK_ROOT?

java - java中的URL检查resume方法

android - 动态ListView几个item响应点击

android - 当按下对话框删除按钮时,如何从数据库中删除 ListView 项?

java - 替换ListView的 'column'中的数据

android - 如何从 native 端的 native 模块获取变量

java - 用户登录 Android Studio 后如何将应用程序打开到不同的 Activity

android - 尝试执行自定义 ListView ,但当我尝试连接数据库时出现错误

android - 在 Cursor 支持的 RecyclerView 中隐藏(并重新显示)卡片

android - SimpleCursorAdapter 的 onRestoreInstanceState