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

时间:2014-08-16 13:33:56

标签: java android database sqlite

如何按ID检索特定数据并在textview中显示?而不是检索数据并显示listview中的所有数据。请帮忙!

公共类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 Cursor 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;

}

1 个答案:

答案 0 :(得分:0)

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

例如,如果您想要返回第一条记录,请使用c.moveToPosition(0),使用c.moveToPosition(1)进行第二次记录,依此类推

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