如何使用游标适配器使用具有自定义布局的微调器?

时间:2016-10-31 03:27:41

标签: java android android-layout android-cursoradapter

我的目标是将一个工作微调器(通过游标适配器填充)转换为具有交替背景。类似于: -

enter image description here

目前我有这个,一切正常: -

enter image description here

这是游标adpater中的相关工作代码(即使用普通下拉列表): -

@Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        return LayoutInflater.from(context).inflate(R.layout.activity_aisle_shop_list_selector, parent, false);
    }
    @Override
    public void bindView(View view,Context context, Cursor cursor) {
        determineViewBeingProcessed(view,"BindV",-1);

        TextView shopname = (TextView) view.findViewById(R.id.aaslstv01);
        shopname.setText(cursor.getString(shops_shopname_offset));

    }

我尝试添加 getDropDownView 的覆盖(代码如下)。我得到了我想要的交替行颜色,但是下拉视图是空白的。但是,如果我在选择器外部单击,那么它们将填充数据(因此我设法获得了上面显示的屏幕截图,我想要的内容)。选择设置正确的项目。

如果我在对布局进行充气后删除 return ,则会填充下拉视图,但会显示其他行的数据(但选择选择正确的项目)

public View getDropDownView(int position, View convertview, ViewGroup parent) {
        View v = convertview;
        determineViewBeingProcessed(v,"GetDDV",position);
        if( v == null) {
            v = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_aisle_shop_list_entry, parent, false);
            return v;
        }
        Context context = v.getContext();

        TextView shopname = (TextView) v.findViewById(R.id.aasletv01);

        shopname.setText(getCursor().getString(shops_shopname_offset));

        if(position % 2 == 0) {
            v.setBackgroundColor(ContextCompat.getColor(context,R.color.colorlistviewroweven));
        } else {
            v.setBackgroundColor(ContextCompat.getColor(context,R.color.colorlistviewrowodd));
        }
        return v;
    }

1 个答案:

答案 0 :(得分:0)

线索是他们我只是想不够努力。问题是光标位于错误的位置,因为需要通过getCursor()获取光标。

此外,充气后的 return 还为时过早(已被注释掉)。

在从光标访问数据之前添加 getCursor().moveToPosition(position); 可以解决问题。

或者(也许更准确地说,评论是否一种方法比另一种方法更正确)。中加入: -

    Cursor cursor = getCursor();
    cursor.moveToPosition(position);

然后将getCursor()替换为cursor非强制性)也可以。

因此 getDropDownView 方法的最终代码可能是: -

public View getDropDownView(int position, View convertview, ViewGroup parent) {
        View v = convertview;
        determineViewBeingProcessed(v,"GetDDV",position);
        if( v == null) {
            v = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_aisle_shop_list_entry, parent, false);
            //return v;
        }
        Context context = v.getContext();
        Cursor cursor = getCursor();
        cursor.moveToPosition(position);


        TextView shopname = (TextView) v.findViewById(R.id.aasletv01);

        shopname.setText(cursor.getString(shops_shopname_offset));

        if(position % 2 == 0) {
            v.setBackgroundColor(ContextCompat.getColor(context,R.color.colorlistviewroweven));
        } else {
            v.setBackgroundColor(ContextCompat.getColor(context,R.color.colorlistviewrowodd));
        }
        return v;
    }
相关问题