0

我已经编写了从数据库中获取数据并将其绑定到列表视图的代码,现在我想从列表视图中单击特定项目并获取被单击的项目的名称。

Cursor cursor = dbHelper.fetchAllRecords();
        String[] columns = new String[] {
                RecordsDbAdapter.KEY_NAME,
                RecordsDbAdapter.KEY_BIRTHDAY,

        };
        int[] to = new int[] {
                R.id.name,
                R.id.birthdate,
        };
        dataAdapter = new SimpleCursorAdapter(
                this, R.layout.row,
                cursor,
                columns,
                to);
        ListView listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(dataAdapter);
        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // When clicked, show a toast with the TextView text
                Toast.makeText(getApplicationContext(),"cominggggg", Toast.LENGTH_SHORT).show();

            }
        });
    }
4

2 回答 2

1
Toast.makeText(getApplicationContext(), "Click ListItem Text "
                                + ((TextView) view.findViewById(R.id.Txt))
                                        .getText().toString(), Toast.LENGTH_LONG).show();

您可以显示带有所选项目名称的吐司

于 2012-10-26T11:10:34.313 回答
0

像这样编辑你的代码:-

listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // When clicked, show a toast with the TextView text
            TextView textView = (TextView) view;
            Toast.makeText(getApplicationContext(),textView.getText().toString(), Toast.LENGTH_SHORT).show();

        }
    });
于 2012-10-26T11:36:34.943 回答