简体   繁体   English

如何从特定的行SQLiteAssetHelper检索数据

[英]How to retrieve data from specific row SQLiteAssetHelper

how to retrieve specific data by their Id and display them in textview? 如何通过其ID检索特定数据并在textview中显示它们? instead of retrieving the data and display all the data in listview. 而不是检索数据并在列表视图中显示所有数据。 please help! 请帮忙!

public class KaikaiProfileActivity extends ListActivity { 公共类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();
}

// retrieve all data how to retrieve the data from specific row? //检索所有数据如何从特定行检索数据? public Cursor getAnimals() { 公共游标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;

}

use c.moveToPosition(int index) in place of c.moveToFirst() to get record at that particular index 使用c.moveToPosition(int index)代替c.moveToFirst()以获取该特定索引处的记录

For example, if you want to return first record, use c.moveToPosition(0) , for second record use c.moveToPosition(1) and so on 例如,如果要返回第一条记录,请使用c.moveToPosition(0) ,第二条记录请使用c.moveToPosition(1) ,依此类推

If you want to retrieve by column value then use qb.appendWhere("<colname>=<value>") ; 如果qb.appendWhere("<colname>=<value>") ;列值检索,请使用qb.appendWhere("<colname>=<value>") ;

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

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