2
我有一个自定义的listview行,其中包含一些textView组件。我没有使用“标准”单一文本项目,而是创建了一个项目,其中每个列表视图行包含几位信息。对于这个例子,我有一个记录ID,一个名称和一个列表视图中的每一行的描述。Android自定义ListView选择的项目的孩子?
我必须通过
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);
填充的ListView我现在我要的是能够给每个被点击的项目中访问的recordId值。我试图遵循Android教程无济于事。他们做类似
Object o = adapter.getItemAtPosition(position);
但仍然没有帮助。我真正想要的是获取recordId的值在每个选定的listItem中。有谁知道这将如何完成?这是我的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);
}
};
感谢您的回复。我对java还是比较陌生,并没有弄清楚所有的错综复杂。所以你说没有子类化的话,没有办法在适配器或textview中访问这些值。在我看来,这些值是可访问的,因为我们将适配器和视图都传递给onListItemCLick处理程序。当我放置监视表达式时,可以看到适配器中的列,但无法获取每列中保存的值。有点困惑,为什么我们需要适配器和视图作为参数,如果他们不能用于检索数据值? – Shawn 2010-10-27 00:59:50
我确定他们是可以接受的。只是你可能不得不做更多的事情,而不是上课。基类是作为一个框架。在做任何事情而不仅仅是显示值时,你都期望适配器的子类 – Falmarri 2010-10-27 02:37:10