0

我有一个 ListView 并且需要检查已单击了哪个项目。我拥有它的方式,它只在我使用鼠标滚轮时显示我选择的项目(在 Eclipse 模拟器中测试它时)。当我在真正的移动设备上测试它时,它根本没有反应。

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
listView.setAdapter(adapter);

listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
        myFunction(listView.getSelectedItem());
    }

    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
    }
});
4

2 回答 2

1

使用setOnItemClickListener 代替 setOnItemSelectedListener

listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
            //get value for listItems using position    

                     String str =listItems[position];                   
                     myFunction(str);
            }
        });
于 2012-05-30T13:31:02.887 回答
0

我认为你需要在 onListItemClick 监听器上使用更好

     public void onListItemClick(ListView parent, View v, int position, long id)
              {
                parent.setItemChecked(position, parent.isItemChecked(position));
         Toast.makeText(this,  "You have selected" + items[position],30).show();
                }
于 2012-05-30T13:32:49.410 回答