微调器项目的背景颜色随机更改



我有一个微调器,其中包含从数据库查询填充的项目列表。我正在根据在本地 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);

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

无论条件如何,我每次都会返回转换视图。我所做的是,只有当 db id 与 id 匹配时,我才返回转换视图,否则我将转换视图返回为 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);

最新更新