我已经从 SQLite 数据库创建了一个 ListView,但我一直坚持如何为每个 ListView 项目添加一个侦听器,以便在单击一个项目时我可以显示另一个页面,其中包含有关该项目的更多信息。该数据库只是一个示例。任何帮助,将不胜感激。
public class Database extends ListActivity {
private final String SAMPLE_DB_NAME = "myFriendsDb";
//private final String SAMPLE_TABLE_NAME = "friends";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> results = new ArrayList<String>();
SQLiteDatabase db = null;
try {
db = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS people" +
" (LastName VARCHAR, FirstName VARCHAR," +
" Country VARCHAR, Age INT(3));");
db.execSQL("INSERT INTO people" +
" Values ('Jones','Bob','UK',30);");
db.execSQL("INSERT INTO people" +
" Values ('Smith','John','UK',40);");
db.execSQL("INSERT INTO people" +
" Values ('Thompson','James','UK',50);");
Cursor c = db.rawQuery("SELECT FirstName, LastName FROM people", null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String firstName = c.getString(c.getColumnIndex("FirstName"));
String lastName = c.getString(c.getColumnIndex("LastName"));
results.add("" + firstName + " " + lastName);
}while (c.moveToNext());
}
}
this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} finally {
if (db != null)
db.execSQL("DELETE FROM people");
db.close();
}
}
}