Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从数据库内容(SQLite)填充微调器_Java_Android_Sqlite_Spinner - Fatal编程技术网

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

Java 从数据库内容(SQLite)填充微调器,java,android,sqlite,spinner,Java,Android,Sqlite,Spinner,如何从数据库(SQLite)填充微调器内容 我有POJO:类别,包含id和名称, 我已经有了表,有一个函数可以像这样获取ArrayList: public List<SetcardCategory> getAllSetcardCategory() { List<SetcardCategory> setcardCategories = new ArrayList<SetcardCategory>(); String selectQuery = "

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

我有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;
}
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);
公共列表getAllSetcardCategory()
{
List setcardCategories=new ArrayList();
String selectQuery=“SELECT*FROM”+表格\设置卡片\类别;
SQLiteDatabase db=this.getReadableDatabase();
游标c=db.rawQuery(selectQuery,null);
//循环遍历所有行并添加到列表
if(c.moveToFirst()){
做{
SetcardCategory SetcardCategory=新的SetcardCategory();
setcardCategory.setId(c.getInt((c.getColumnIndex(“id”)));
setcardCategory.setName(c.getString(c.getColumnIndex(“name”));
//添加到标记列表
setcardCategories.add(setcardCategories);
}而(c.moveToNext());
}
返回设置卡片类别;
}
然后在活动中,我这样称呼它:

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);
List setcardCategories=db.getAllSetCardCategories();
ArrayAdapter ArrayAdapter=新的ArrayAdapter(
这个,android.R.layout.simple_微调器_项目,setcardCategories);
arrayAdapter.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
微调器站点=(微调器)findViewById(R.id.setcardCategory);
sItems.setAdapter(arrayAdapter);
当我运行它时,它加载如下字符串:“schema。SetcardCategory@22293c98“还有许多其他类似的价值观

如何填充微调器以将name字段显示为标签,将id字段显示为要保存到DB中的值

class Pojo{
 private String name;
  @Override
    public String toString() {
        return name;
    }
}
在pojo类中这样做,这样当对象在适配器中使用ToString方法加载数据时,它将返回一个值


在pojo类中这样做,这样当对象在适配器中使用to string方法加载数据时,它将为对象返回一个值 覆盖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;
    }
公共列表getAllSetcardCategory()
{
List setcardCategories=new ArrayList();
String selectQuery=“SELECT*FROM”+表格\设置卡片\类别;
SQLiteDatabase db=this.getReadableDatabase();
游标c=db.rawQuery(selectQuery,null);
//循环遍历所有行并添加到列表
if(c.moveToFirst()){
做{
//添加到标记列表
添加(c.getString(c.getColumnIndex(“name”));
}而(c.moveToNext());
}
返回设置卡片类别;
}
并将阵列适配器创建为

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);
List setcardCategories=db.getAllSetCardCategories();
ArrayAdapter ArrayAdapter=新的ArrayAdapter(
这个,android.R.layout.simple_微调器_项目,setcardCategories);
arrayAdapter.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);

解决方案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;
    }
公共列表getAllSetcardCategory()
{
List setcardCategories=new ArrayList();
String selectQuery=“SELECT*FROM”+表格\设置卡片\类别;
SQLiteDatabase db=this.getReadableDatabase();
游标c=db.rawQuery(selectQuery,null);
//循环遍历所有行并添加到列表
if(c.moveToFirst()){
做{
//添加到标记列表
添加(c.getString(c.getColumnIndex(“name”));
}而(c.moveToNext());
}
返回设置卡片类别;
}
并将阵列适配器创建为

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);
List setcardCategories=db.getAllSetCardCategories();
ArrayAdapter ArrayAdapter=新的ArrayAdapter(
这个,android.R.layout.simple_微调器_项目,setcardCategories);
arrayAdapter.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);

在pojo类overide tostring方法中,返回要在喷丝头中显示的值您可以使用
SimpleCursorAdapter在pojo类overide tostring方法中仅在5行代码中执行此操作,并返回要在喷丝头中显示的值您可以使用
SimpleCursorAdapter
使用这个,我无法获取id,对吗?Manoj kumar给出的答案更适合这两种情况。我给出了第二个解决方案,因为看起来您只需要名称。哦,是的,我很抱歉,我先看到了Manoj的答案,但实际上您的答案是最先发布的。@Gabriel否,它在适配器中没有提供有效的行ID,因此您应该使用
CursorAdapter
-例如
SimpleCursorAdapter
,通过使用此选项,我拿不到身份证,对吗?Manoj kumar给出的答案更适合这两种情况。我给出了第二个解决方案,因为看起来您只需要名称。哦,是的,我很抱歉,我先看到了Manoj的答案,但实际上您的答案是最先发布的。@Gabriel否,它在适配器中没有提供有效的行ID,因此您应该使用
CursorAdapter
-例如
SimpleCursorAdapter