获取被单击的项目的名称

时间:2012-10-26 10:57:40

标签: android listview

我已经编写了代码来从数据库中获取数据并将其绑定到listview现在我想点击listview中的特定项目并获取被点击的项目的名称。

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();

            }
        });
    }

2 个答案:

答案 0 :(得分:1)

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

您可以使用所选项目名称

显示祝酒词

答案 1 :(得分: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();

        }
    });