gpt4 book ai didi

java - 如何从特定行检索数据 SQLiteAssetHelper

转载 作者:太空宇宙 更新时间:2023-11-04 14:37:49 27 4
gpt4 key购买 nike

如何通过 ID 检索特定数据并将其显示在 TextView 中?而不是检索数据并在 ListView 中显示所有数据。请帮忙!

公共(public)类 KaikaiProfileActivity 扩展了 ListActivity {

private Cursor animals;
private MyDatabase db;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

db = new MyDatabase(this);
animals = db.getAnimals(); // you would not typically call this on the main thread

ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
animals,
new String[] {"animalName","animalPersonality"},
new int[] {android.R.id.text1, android.R.id.text2});;


getListView().setAdapter(adapter);






}

@Override
protected void onDestroy() {
super.onDestroy();
animals.close();
db.close();
}

//检索所有数据如何从特定行检索数据?公共(public)光标 getAnimals() {

    SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

String [] sqlSelect = {"0_id", "animalName", "animalInfo","ImageName","animalGender","animalDOB",
"animalDOB", "animalBirthPlace", "animalPersonality", "animalFeatures", "animalFood", "animalPastTime"
};
String sqlTables = "AnimalInfo";

qb.setTables(sqlTables);
Cursor c = qb.query(db, sqlSelect, null, null,
null, null, null);

c.moveToFirst();
return c;

}

最佳答案

使用c.moveToPosition(int index)代替c.moveToFirst()获取该特定索引处的记录

例如,如果要返回第一条记录,请使用 c.moveToPosition(0) ,用于第二条记录使用c.moveToPosition(1)等等

如果您想按列值检索,请使用 qb.appendWhere("<colname>=<value>") ;

关于java - 如何从特定行检索数据 SQLiteAssetHelper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25340665/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com