简体   繁体   English

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

[英]Populate spinner from database content (SQLite)

How can I populate a spinner content from database (SQLite) 如何从数据库(SQLite)填充微调框内容

I have POJO: categories, contain id and name, I have the table already, with a function to get the ArrayList like this: 我有POJO:类别,包含ID和名称,我已经有表,并具有获取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;
}

Then on Activity I call it like this: 然后在活动上,我这样称呼它:

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);

when I run it, it loads string like this: "schema.SetcardCategory@22293c98" and many others values similar to that. 当我运行它时,它会加载这样的字符串:“ schema.SetcardCategory@22293c98”以及许多与此类似的值。

How can I populate the spinner to show the name field as a label, and id field as the value that we fetch to save into DB? 如何填充微调框,以将名称字段显示为标签,将id字段显示为我们获取以保存到DB中的值?

class Pojo{
 private String name;
  @Override
    public String toString() {
        return name;
    }
}

do it like this in the pojo class, so this will return a value for the object when it uses the to string method in the adapter, to load the data 在pojo类中这样做,因此当它在适配器中使用to字符串方法来加载数据时,它将为该对象返回一个值。

Solution 1 Overide the toString method in your SetcardCategory class 解决方案1覆盖SetcardCategory类中的toString方法

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

Solution 2 If you just want to show the name, Just pick name only from DB 解决方案2如果仅想显示名称,则仅从数据库中选择名称

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;
    }

And create Array Adapter as 并创建阵列适配器为

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM