Android Custom ListView选择项目的孩子?

时间:2010-10-27 00:12:18

标签: android listview adapter listviewitem listadapter

我有一个自定义列表视图行,其中包含许多textView组件。我创建了一个项目,而不是“标准”单个文本项,其中每个listview行包含几个信息位。对于此示例,我有listview中每行的记录ID,名称和描述。

我的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);

现在我想要的是能够访问每个被点击的项目中的recordId值。我试过按照Android教程无济于事。他们做的事情就像

Object o = adapter.getItemAtPosition(position);

但这仍然无济于事。我真正想要的是获取每个选定listItem的recordId WITHIN的值。有谁知道这将如何实现?这是我的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);
            }       
        };

1 个答案:

答案 0 :(得分:0)

你试图从默认的适配器类中做太多。您需要编写自己的适配器类,扩展simplecursoradapter,然后您可以在任何您想要的位置制作任何您想要的方法。

相关问题