Вот как я решил проблему.Я получил employee_ids и employee_names из локальной базы данных SQLite, затем я создал ArrayList employeeNamesArray и ArrayList employeeIdArray одновременно.Таким образом, 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
}
}