我有一个名为 plethora.db 的数据库。我在我的应用程序中使用它。我已经通过 数据库浏览器for SQLite 预先填充了数据库,因为我输入的数据太多而且有用且速度更快。
问题我面临:
ListView
中id = 2的所有元素,但我的荣誉6x中ListView
为空,而logcat没有任何错误。MyDatabase.java
public class MyDatabase extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "plethora.db";
private static final int DATABASE_VERSION = 1;
public MyDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public Cursor getNames() {
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String [] sqlSelect = {"2 _id", "Name"};
String sqlTables = "Names";
String selection = "_id = ?";
String[] selectionArgs = { "2" };
qb.setTables(sqlTables);
Cursor c = qb.query(db, sqlSelect, selection, selectionArgs,
null, null, null);
c.moveToFirst();
return c;
}
}
MainActivity.java
db = new MyDatabase(this);
names = db.getNames();
final ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
names,
new String[] {"Name"},
new int[] {android.R.id.text1});
final ListView listView = (ListView) findViewById(R.id.listV2);
listView.setAdapter(adapter);