使用CursorAdapter在bindView中获得位置 [英] get a position in bindView using CursorAdapter

查看:141
本文介绍了使用CursorAdapter在bindView中获得位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个具有两个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

          }

解决方案

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

}

i am create a listview which has two TextView and one custom view as a selection indicator , to load data i am using CursorAdapter and overriding bindView and newView. but the issue is in preserving the selection index.

when i am tapping a list item , i am saving the position of it into view as follows

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

but inside a bindView i not getting a position where i can perform a match by fetching position from view's tag as follows

 Integer position = (Integer) v1.getTag();

but i am not getting a position to compare just the way we get in getView. i tried cur.getPosition() is the position of a record in cursor which wont match with the v1.getTag().

i tried overriding getView as follows , but not getting accurate position

@Override
public View getView(final int position, View convertView, ViewGroup parent){

    this.position = position;
    return super.getView(position, convertView, parent);


}

so how i can i get the position in bindView ? do i need to use getView or what ? i explored many threads but i didn't get any definite answer when it comes to bindView.

EDIT

        @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

          }

解决方案

For This first you need to override getView() and set the position here and access it in bindview() later.

@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);
}

and in bindView() get your position like

@Override
public void bindView(View view, Context context, Cursor cursor) {
    int position=(Integer) view.getTag();//here is the position

}

这篇关于使用CursorAdapter在bindView中获得位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆