大家好,我在对话框中设置了ListView,但是当我在对话框中设置onItemSelectedListener时,它甚至没有注册在对话框的ListView中选择了任何东西!有任何想法吗?这是我所拥有的:

 Dialog dialog = new Dialog(DealActivity.this);
 dialog.setContentView(R.layout.test_list);
 dialog.setTitle("Test Dialog");
 dialog.setCancelable(true);
 dialog.setCanceledOnTouchOutside(true);
 ListView listView = (ListView) dialog.findViewById(R.id.listView);

 //DIDNT INCLUDE CODE HERE THAT SETS UP THE ADAPTER!
 //THIS IS WHERE I SET UP THE LISTENER WITHIN THE DIALOG.

 listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

     public void onItemSelected(AdapterView<?> parent,View arg1, int arg2, long arg3) {
        Log.d("clicked","clicked "+ listView.getItemSelectedPosition());
    }

    public void onNothingSelected(AdapterView<?> parent) {

    }
});
}
    dialog.show();
}

最佳答案

尝试这个,

ListView list = (ListView) findViewById(R.id.listView);
list.setItemsCanFocus(false);


如果ListView中的每个项目都有一些控件,例如CheckBox,Button等,请将其添加到其布局xml中。

android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"

关于android - 如何在对话框中设置ListView的“onItemSelectedListener”。安卓系统,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6721838/

10-09 12:53