これが私が問題を解決した方法です。ローカルの SQLite データベースから employee_ids と employee_names を取得し、employeeNamesArray の ArrayList と employeeIdArray の ArrayList を同時に作成しました。したがって、employeeIdArray[0] は employeeNameArray[0] と一致し、employeeIdArray[1] は employeeNameArray[1] と一致します。
ArrayLists が作成されたら、employeeNameArray を ListView に入力しました。
その後、onListItemClick で、選択した ListView 行の位置を取得します。この「位置」は ArrayLists の位置に対応します。したがって、ListView の最初の行を選択すると、位置はゼロになり、employeeNameArray[0] は employeeIdArray[0] と一致します。employeeIdArray から相関エントリを取得し、putExtra を使用して次のアクティビティにプッシュします。
public class MyFirstDatabase extends ListActivity {
ArrayList<String> employeeIdArray = new ArrayList<String>(); // List of EmployeeIDs
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Open the database
SQLiteDatabase db;
db = openOrCreateDatabase("mydb.db",SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
// Query the database
Cursor cur = db.query("employee", null, null, null, null, null, "employee_lastname");
cur.moveToFirst(); // move to the begin of the db results
ArrayList<String> employeeNameArray = new ArrayList<String>(); // Initialize mArrayList
while (cur.isAfterLast() == false) {
employeeNameArray.add(cur.getString(1)); // add the employee name to the nameArray
employeeIdArray.add(cur.getString(0)); // add the employee id to the idArray
cur.moveToNext(); // move to the next result set in the cursor
}
cur.close(); // close the cursor
// put the nameArray into the ListView
setListAdapter(new ArrayAdapter<String>(this,R.layout.list_item,employeeNameArray));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
}
protected void onListItemClick(ListView l, View v, final int position, long id) {
super.onListItemClick(l, v, position, id);
Intent myIntent = new Intent(this, SubView.class); // when a row is tapped, load SubView.class
Integer selectionID = Integer.parseInt(employeeIdArray.get(position)); // get the value from employeIdArray which corrosponds to the 'position' of the selected row
myIntent.putExtra("RowID", selectionID); // add selectionID to the Intent
startActivityForResult(myIntent, 0); // display SubView.class
}
}