Android SQLite:微调器 + select sql 方法
Android SQLite: Spinner + select sql methods
我有一个显示 SQLite 数据的 Spinner。为此,我正在使用这种 select 方法:
public List<String> getAllProductsName(int id)
{
String buildSQL = "SELECT nome FROM " + DatabaseHelper.Produtos.TABELA + " WHERE id =" + id;
List<String> nomes = new ArrayList<String>();
SQLiteDatabase db = this.getDatabase();
Cursor cursor = database.rawQuery(buildSQL, null);
if (cursor.moveToFirst()) {
do {
nomes.add(cursor.getString(0));
} while (cursor.moveToNext());
}
return nomes;
}
问题是,我只得到了名字,但我还需要 ID。我知道我可以使用 "SELECT nome, _id FROM ",但我将如何 return 呢?我可以用相同的方法 return 2 个列表(一个带有 IDS,另一个带有名称)吗?
或者也许我应该创建一个仅显示名称的新方法(当我将 ID 作为参数提供时)?请帮忙!提前致谢! :)
这样的东西怎么样...使用并返回 HashMap,其中包含 ID 作为键 和 nome 作为值
public HashMap<Integer,String> getAllProductsName(int id)
{
String buildSQL = "SELECT nome,_id FROM " + DatabaseHelper.Produtos.TABELA + " WHERE id =" + id;
HashMap<Integer,String> idAndNomes = new HashMap<Integer,String>();
SQLiteDatabase db = this.getDatabase();
Cursor cursor = database.rawQuery(buildSQL, null);
if (cursor.moveToFirst()) {
do {
idAndNomes.put(cursor.getInt(1), cursor.getString(0)));
} while (cursor.moveToNext());
}
return idAndNomes;
}
那么你可以使用:
idAndNomes.keySet()
- Returns 此映射中包含的一组键。在我们的例子中 ID.
idAndNomes.values()
- Returns 此映射中包含的值的集合。在我们的例子中 nomes。
我有一个显示 SQLite 数据的 Spinner。为此,我正在使用这种 select 方法:
public List<String> getAllProductsName(int id)
{
String buildSQL = "SELECT nome FROM " + DatabaseHelper.Produtos.TABELA + " WHERE id =" + id;
List<String> nomes = new ArrayList<String>();
SQLiteDatabase db = this.getDatabase();
Cursor cursor = database.rawQuery(buildSQL, null);
if (cursor.moveToFirst()) {
do {
nomes.add(cursor.getString(0));
} while (cursor.moveToNext());
}
return nomes;
}
问题是,我只得到了名字,但我还需要 ID。我知道我可以使用 "SELECT nome, _id FROM ",但我将如何 return 呢?我可以用相同的方法 return 2 个列表(一个带有 IDS,另一个带有名称)吗?
或者也许我应该创建一个仅显示名称的新方法(当我将 ID 作为参数提供时)?请帮忙!提前致谢! :)
这样的东西怎么样...使用并返回 HashMap,其中包含 ID 作为键 和 nome 作为值
public HashMap<Integer,String> getAllProductsName(int id)
{
String buildSQL = "SELECT nome,_id FROM " + DatabaseHelper.Produtos.TABELA + " WHERE id =" + id;
HashMap<Integer,String> idAndNomes = new HashMap<Integer,String>();
SQLiteDatabase db = this.getDatabase();
Cursor cursor = database.rawQuery(buildSQL, null);
if (cursor.moveToFirst()) {
do {
idAndNomes.put(cursor.getInt(1), cursor.getString(0)));
} while (cursor.moveToNext());
}
return idAndNomes;
}
那么你可以使用:
idAndNomes.keySet()
- Returns 此映射中包含的一组键。在我们的例子中 ID.
idAndNomes.values()
- Returns 此映射中包含的值的集合。在我们的例子中 nomes。