【问题标题】:Background color of spinner item gets randomly change微调器项目的背景颜色随机更改
【发布时间】:2018-01-22 07:15:04
【问题描述】:

我有一个微调器,其中包含从数据库查询中填充的项目列表。我正在根据本地 sqlite 数据库中找到的数据更改微调器项目的背景颜色。一切正常,除了当我在微调器中滚动列表时,其他不应更改其背景颜色的项目也更改其背景颜色。

我很清楚 android listview 中的回收问题,并且我已经实现了持有人模式来解决它,但我在解决我的微调列表中的问题时遇到了困难。

这是我的代码:

ArrayAdapter<Category> categoryAdapter = new ArrayAdapter<Category>(getActivity(),
        android.R.layout.simple_dropdown_item_1line, categoryList){
    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View row = super.getDropDownView(position, convertView, parent);

            Category category = (Category) getItem(position);
            Cursor crsCheckCatAva = database.rawQuery("SELECT distinct category_id FROM "+ ItemsTable.TABLE_OUTLET_DATA +" WHERE "+ ItemsTable.COLUMN_OUTLET_DATA_OUTLET_ID +"='"+ Info.getInstance().getOutletID() +"'", null);
            if(crsCheckCatAva.getCount() > 0){
                while (crsCheckCatAva.moveToNext()){
                    if(category.getCategory_id() == 0){
                        row.setBackgroundColor(Color.WHITE);
                    }else
                    if(crsCheckCatAva.getInt(crsCheckCatAva.getColumnIndex("category_id")) == category.getCategory_id()){
                        row.setBackgroundColor(Color.GRAY);
                    }
                }
            }
            crsCheckCatAva.close();
        return row;
    }
};
categorySelectionSpinner.setAdapter(categoryAdapter);

【问题讨论】:

  • 设计您自己的自定义适配器。通过将 cusor 添加到列表并将最终列表传递给适配器,在活动线程中执行 db 进程。

标签: android android-spinner


【解决方案1】:

我找到了解决方案,这就是我所做的:

无论条件如何,我每次都返回 convertView。我所做的是仅当 db id 与 id 匹配时才返回 convertView,否则我将 convertview 返回为 null。这是我的代码:

ArrayAdapter<Category> categoryAdapter = new ArrayAdapter<Category>(getActivity(),
                android.R.layout.simple_dropdown_item_1line, categoryList){
            @Override
            public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                View row = super.getDropDownView(position, null, parent);

                    Category category = (Category) getItem(position);
                    Cursor crsCheckCatAva = database.rawQuery("SELECT distinct category_id FROM "+ ItemsTable.TABLE_OUTLET_DATA +" WHERE "+ ItemsTable.COLUMN_OUTLET_DATA_OUTLET_ID +"='"+ Info.getInstance().getOutletID() +"'", null);
                    if(crsCheckCatAva.getCount() > 0){
                        while (crsCheckCatAva.moveToNext()){
                            /*if(category.getCategory_id() == 0){
                                row.setBackgroundColor(Color.WHITE);
                            }else*/
                            if(crsCheckCatAva.getInt(crsCheckCatAva.getColumnIndex("category_id")) == category.getCategory_id()){
                                row = super.getDropDownView(position, convertView, parent);
                                row.setBackgroundColor(Color.GRAY);
                            }
                        }
                    }else {
                        row = super.getDropDownView(position, null, parent);
                    }
                    crsCheckCatAva.close();
                return row;
            }
        };
        categorySelectionSpinner.setAdapter(categoryAdapter);

【讨论】:

    猜你喜欢
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多