微调项目的背景颜色随机变化

时间:2018-01-22 07:15:05

标签: android android-spinner

我有一个微调器,它有一个从db查询填充的项目列表。我正在根据我本地sqlite db中的数据更改微调器项的背景颜色。一切都工作正常,除非我在我的微调器中滚动列表,其他不应改变其背景颜色的项目,也改变它的背景颜色。

我很清楚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);

1 个答案:

答案 0 :(得分: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);