我有一个包含许多 textView 组件的自定义 ListView 行。我创建了一个项目,其中每个 ListView 行都包含几位信息,而不是“标准”的单个文本项目。对于这个例子,我有一个记录 ID、一个名称和 ListView 中每一行的描述。
我通过
填充了我的 ListViewthis.mDbHelper = new RecordDBAdapter(this);
this.mDbHelper.open();
Cursor c = this.mDbHelper.fetchAllRecords();
startManagingCursor(c);
String[] from = new String[] {RecordDBAdapter.ROW_ID, RecordDBAdapter.NAME, RecordDBAdapter.DESCRIPTION};
int[] to = new int[] {R.id.recordId, R.id.lblName, R.id.lblDescription};
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter records = new SimpleCursorAdapter(this, R.layout.record_row, c, from, to);
this.list.setAdapter(dives);
现在我想要的是能够访问每个单击项目中的 recordId 值。我试图按照 Android 教程进行操作,但无济于事。他们做类似的事情
Object o = adapter.getItemAtPosition(position);
但这仍然无济于事。我真正想要的是在每个选定的 listItem 中获取 recordId 的值。有谁知道这将如何实现?这是我的 onItemClick 事件:
protected OnItemClickListener onListItemClick = new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long rowId) {
// My goal is either to grab the value of the
// recordId value from the textView, or from the adapter.
//Object o = adapter.getItemAtPosition(position); //Tried this, no joy
Intent intent = new Intent(NeatActivity.this, SomeOtherActivity.class);
// intent.putExtra("selectedRecordId",val); //val here is a numerical record row id being passed to another activity.
startActivity(intent);
}
};
最佳答案
您试图在默认适配器类之外做太多事情。你需要编写自己的适配器类,扩展simplecursoradapter
,然后你可以在你想要的任何位置制作你想要的任何方法来获取你想要的任何东西。
关于Android 自定义ListView 选中项子项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4029047/