根据sqlite数据更改微调器项的背景

时间:2018-01-15 14:05:43

标签: android spinner android-spinner custom-adapter

我正在根据我的SQLite查询提供的数据更改我的微调器中项目的背景颜色。我使用getDropDownView方法检查数据并更改背景颜色。

现在让我假设我的SQLite表上有2条记录,其中包含ID 1和2.我面临的问题是只有最后一条记录(在本例中为id为2的记录)才能获得背景颜色。这就是我正在做的事情:

    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 = null;
                    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(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);
                                row.setBackgroundColor(Color.WHITE);
                            }
                        }
                    }else {
                        row = super.getDropDownView(position, null, parent);
                    }
                    crsCheckCatAva.close();

                    return row;
                }
            };
 categorySelectionSpinner.setAdapter(categoryAdapter);

这是输出:

01-15 19:19:16.361 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 0
01-15 19:19:16.362 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 0
01-15 19:19:16.363 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 4
01-15 19:19:16.363 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 4
01-15 19:19:16.364 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 6
01-15 19:19:16.365 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 6
01-15 19:19:16.366 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 7
01-15 19:19:16.366 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 7
01-15 19:19:16.368 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 8
01-15 19:19:16.368 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 8
01-15 19:19:16.370 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 26
01-15 19:19:16.371 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 26
01-15 19:19:16.372 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 27
01-15 19:19:16.372 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 27
01-15 19:19:16.373 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 28
01-15 19:19:16.374 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 28
01-15 19:19:16.375 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 29
01-15 19:19:16.375 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 29
01-15 19:19:16.379 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 30
01-15 19:19:16.381 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 30
01-15 19:19:16.382 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 31
01-15 19:19:16.382 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 31
01-15 19:19:16.386 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 0
01-15 19:19:16.387 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 0
01-15 19:19:16.388 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 4
01-15 19:19:16.388 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 4
01-15 19:19:16.389 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 6
01-15 19:19:16.390 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 6
01-15 19:19:16.391 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 7
01-15 19:19:16.391 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 7
01-15 19:19:16.394 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 8
01-15 19:19:16.394 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 8
01-15 19:19:16.396 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 26
01-15 19:19:16.397 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 26
01-15 19:19:16.398 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 27
01-15 19:19:16.399 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 27
01-15 19:19:16.401 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 28
01-15 19:19:16.401 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 28
01-15 19:19:16.403 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 29
01-15 19:19:16.404 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 29
01-15 19:19:16.405 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 30
01-15 19:19:16.405 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 30
01-15 19:19:16.407 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 31
01-15 19:19:16.407 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 31
01-15 19:19:16.656 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 6
01-15 19:19:16.658 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 6
01-15 19:19:16.658 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 7
01-15 19:19:16.659 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 7
01-15 19:19:16.660 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 8
01-15 19:19:16.660 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 8
01-15 19:19:16.662 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 26
01-15 19:19:16.662 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 26
01-15 19:19:16.663 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 27
01-15 19:19:16.664 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 27
01-15 19:19:16.665 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 28
01-15 19:19:16.665 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 28
01-15 19:19:16.674 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 29
01-15 19:19:16.675 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 29
01-15 19:19:16.676 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 30
01-15 19:19:16.676 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 30
01-15 19:19:16.678 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 31
01-15 19:19:16.678 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 31
01-15 19:19:16.679 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 4
01-15 19:19:16.679 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 4
01-15 19:19:16.680 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 4, VIEW ID: 0
01-15 19:19:16.681 7596-7596/? I/OUTPUT: getDropDownView: DB ID: 6, VIEW ID: 0

1 个答案:

答案 0 :(得分:0)

这里的问题是,您的查询返回多个结果(似乎每个查询返回2个结果,数据库ID:4 数据库ID:6 ) ,并且您正在迭代getDropDownView方法中的光标。

因此,这会使您的下拉视图的背景颜色始终第二次符合if-else语句条件的值。