代码之家  ›  专栏  ›  技术社区  ›  Gabriel

从数据库内容填充微调器(SQLite)

  •  0
  • Gabriel  · 技术社区  · 7 年前

    如何从数据库(SQLite)填充微调器内容

    我已经有了这个表,有一个函数可以像这样获取ArrayList:

    public List<SetcardCategory> getAllSetcardCategory()
    {
        List<SetcardCategory> setcardCategories = new ArrayList<SetcardCategory>();
        String selectQuery = "SELECT  * FROM " + TABLE_SETCARD_CATEGORIES;
    
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery(selectQuery, null);
    
        // looping through all rows and adding to list
        if (c.moveToFirst()) {
            do {
                SetcardCategory setcardCategory = new SetcardCategory();
                setcardCategory.setId(c.getInt((c.getColumnIndex("id"))));
                setcardCategory.setName(c.getString(c.getColumnIndex("name")));
    
                // adding to tags list
                setcardCategories.add(setcardCategory);
            } while (c.moveToNext());
        }
        return setcardCategories;
    }
    

    List<SetcardCategory> setcardCategories = db.getAllSetcardCategory();
        ArrayAdapter<SetcardCategory> arrayAdapter = new ArrayAdapter<SetcardCategory>(
                this, android.R.layout.simple_spinner_item, setcardCategories);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        Spinner sItems = (Spinner) findViewById(R.id.setcardCategory);
        sItems.setAdapter(arrayAdapter);
    

    当我运行它时,它加载如下字符串:“schema。SetcardCategory@22293c98“还有许多其他类似的价值观。

    如何填充微调器以将名称字段显示为标签,将id字段显示为我们获取并保存到DB中的值?

    2 回复  |  直到 7 年前
        1
  •  3
  •   Manoj kumar    7 年前
    class Pojo{
     private String name;
      @Override
        public String toString() {
            return name;
        }
    }
    

    在pojo类中这样做,这样当对象在适配器中使用to string方法加载数据时,它将返回一个值

        2
  •  2
  •   J.R    7 年前

    解决方案1 覆盖SetcardCategory类中的toString方法

    class SetcardCategory {
    ...
    ...
    @Override
        public String toString() {
            return this.name;
        }
    }
    

    解决方案2 如果您只想显示名称,只需从DB中选择名称即可

    public List<String> getAllSetcardCategory()
        {
            List<String> setcardCategories = new ArrayList<String>();
            String selectQuery = "SELECT  * FROM " + TABLE_SETCARD_CATEGORIES;
    
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor c = db.rawQuery(selectQuery, null);
    
            // looping through all rows and adding to list
            if (c.moveToFirst()) {
                do {
                    // adding to tags list
                    setcardCategories.add(c.getString(c.getColumnIndex("name")));
                } while (c.moveToNext());
            }
            return setcardCategories;
        }
    

    并将阵列适配器创建为

    List<String> setcardCategories = db.getAllSetcardCategory();
        ArrayAdapter<SetcardCategory> arrayAdapter = new ArrayAdapter<SetcardCategory>(
                this, android.R.layout.simple_spinner_item, setcardCategories);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);