【问题标题】:get a position in bindView using CursorAdapter使用 CursorAdapter 在 bindView 中获取位置
【发布时间】:2014-12-27 10:05:10
【问题描述】:

我正在创建一个列表视图,其中有两个TextView 和一个自定义视图作为选择指示器,以加载我使用的数据CursorAdapter 并覆盖bindViewnewView。但问题在于保留选择索引。

当我点击一个列表项时,我将它的位置保存到视图中,如下所示

View v1=(View)view.findViewById(R.id.viewColor);
v1.setTag(position)

但在bindView 中,我没有获得可以通过从视图标签中获取位置来执行匹配的位置,如下所示

 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

          }

【问题讨论】:

    标签: android listview android-cursoradapter


    【解决方案1】:

    为此,您首先需要覆盖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
    
    }
    

    【讨论】:

    • 如果我使用 convertView 来存储位置,那么我将在哪里存储项目点击的位置?
    • @Hunt sry..我没有明白你的解释。
    • @Hunt 在项目点击它会给你位置。什么问题??
    • @Hunt 你将在 onitemClick 中做什么。为什么你需要保存??
    • @Hunt 在 ArrayList 或 SparceboleanArray 中维护它
    【解决方案2】:

    不要覆盖 getView,因为您使用 CursorAdapter。您可以在 bindView 中注册 onClickListener。假设您在点击图标时聆听,然后您会这样做:

    @Override
    public void bindView(View vw, Context ctx, final Cursor cursor) {
        /* 
        *   do your binding
        */
        ImageView icon = (ImageView) vw.findViewById(R.id.your_icon);
        icon.setTag(cursor.getPosition());
        icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // here you pass right position to your activity
                ((MainActivity) context).onIconClick((int)v.getTag());  
            }
        });
    }
    

    在你的活动中:

    public void onIconClick(int position) {
        // do your stuff
    }
    

    【讨论】:

      【解决方案3】:

      像下面这样更改您的 onItemClick

      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
          mAdapter.setId(id);
          //Set the selector here
      }
      

      然后在adapter中维护一个id变量,并为其编写setter和getter。然后如下更改您的 bindView() 方法

      @Override
      public void bindView(View view, Context context, Cursor cursor) {
      
          long tempId = cursor.getLong(cursor.getColumnIndex("_id"));
      
          if(tempId==id){
              //Set the selector color here
          }
          else{
              //Set the default color here
          }
      
          //Remaing your logic here
      }
      

      这样你就不需要重写getView()方法了。

      如果你想选择多个视图,你必须维护 ArrayList of ids...

      【讨论】:

      • 光标ID与列表项位置不同。光标也会根据用户要求动态更改,如edittext搜索..
      • 但是光标位置change.id不等于位置。
      • 好的..让我解释清楚。如果你在位置 4 有 10 的列表 id,在位置 5 有 11 的 id。在 bindview 我需要 id 10 的位置 ..那么你将如何给予?通过你的回答,我会变得 10 但不是 4.Op 询问位置而不是 id。
      • @Hunt 的意图是维护列表选择器。好的,如果你想要位置然后保持位置而不是_id,你将通过 cursor.getPosition() 获得 bindView() 中的位置;跨度>
      • 但他问了一个标题为 使用 CursorAdapter 在 bindView 中获得一个位置的问题
      【解决方案4】:

      在“bindView”方法内但在“onClickListener()”之外:

      1. final int position = cursor.getPosition();

      2. 在“onClickListener()”里面:

      cursor.moveToPosition(position); //retrieve any data you want after this

      祝你好运~

      【讨论】:

        猜你喜欢
        • 2012-08-26
        • 2013-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-05
        • 2012-09-22
        • 1970-01-01
        相关资源
        最近更新 更多