在Android的ListView分配ID的行
-
27-09-2019 - |
题
我有一个ListView。当在ListView中的项目被窃听,它加载一个子视图。我想一个ID分配给ListView的每一行,这样我就可以传递ID一起到子视图。如何将特定ID分配给在ListView每一行?
下面是我当前如何加载的ListView:
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, mArrayList));
解决方案
下面就是我解决了这个问题。我从本地SQLite数据库的employee_ids和employee_names,然后,我创建employeeNamesArray的ArrayList,并在同一时间employeeIdArray的ArrayList。因此,employeeIdArray [0]将匹配用employeeNameArray [0],employeeIdArray [1]将匹配employeeNameArray [1]等
一旦的ArrayLists创建,我喂employeeNameArray到ListView中。
后来,在onListItemClick,我retreive所选的ListView行的位置。这个“位置”将corrospond到位置中的ArrayLists - 因此,如果我选择在ListView第一排,位置将为零,并且employeeNameArray [0]与employeeIdArray匹配[0]。我通过使用putExtra抓住从employeeIdArray的coroloating条目和该推到下一个活动。
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
}
}
其他提示
喜克里斯,你已经拥有了位置ID在你的ListView,实现onListItemClick()函数。
protected void onListItemClick(ListView l, View v, final int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(this, "my id to pass along the subview is " + position,Toast.LENGTH_LONG).show();
}
如果你想assing自己的ID使用setTag()
v.setTag("myownID"+position);
您不能做到这一点与标准 ArrayAdapter 你需要延长一个ArrayAdapter并覆盖 getItemId()方法,也许还 hasStableIds()方法。
您接下来要在hasStableIds方法返回true,并生成您的ID在所给你的getItemId方法的位置的项目。
在此花费数小时后,我发现这个最简单的方法是覆盖适配器的bindView并设置包含行对项目_id标记值 - 在我的情况,它是ListView的行中的按钮
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.note, cursor, fromColumns, toViews, 0) {
@Override
// add the _id field value to the button's tag
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
Integer index = cursor.getColumnIndex("_id");
Integer row_id = cursor.getInt(index);
Button button = (Button) view.findViewById(R.id.button_delete_record);
button.setTag(row_id);
}
};