Android 自定义ListView 选定的项目子项?

发布于 2024-09-29 05:05:43 字数 1636 浏览 4 评论 0原文

我有一个自定义列表视图行,其中包含许多 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昔梦 2024-10-06 05:05:43

您试图在默认适配器类之外做太多事情。您需要编写自己的适配器类,扩展 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文