Android 自定义ListView 选定的项目子项?
我有一个自定义列表视图行,其中包含许多 textView 组件。我创建了一个项目,其中每个列表视图行都包含几位信息,而不是“标准”单个文本项目。对于此示例,我在列表视图中的每一行都有一个记录 ID、名称和描述。
我的列表视图通过
this.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);
,但这仍然没有帮助。我真正想要的是获取每个选定列表项中的 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);
}
};
I have a custom listview row that contains a number of textView components. Instead of the "standard" single text item, I have created a item where each listview row contains several bits of information. For this example, I have a record id, a name, and a description for each row in the listview.
I have my listview populated via
this.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);
Now what I want is to be able to access the recordId value within each clicked item. I've tried to follow the Android tutorials to no avail. They do something like
Object o = adapter.getItemAtPosition(position);
but that still doesn't help either. What I REALLY want is to get the value of the recordId WITHIN each selected listItem. Does anyone know how this would be accomplished? Here is my onItemClick event:
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);
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您试图在默认适配器类之外做太多事情。您需要编写自己的适配器类,扩展
simplecursoradapter
,然后您可以创建任何您想要的方法,在您想要的任何位置获取您想要的任何内容。You're trying to do too much out of the default adapter class. You need to write your own adapter class, extend
simplecursoradapter
, and then you can make any method you want to get whatever you want at any position you want.