Android 对话框中的setOnItemClickListener不工作
当用户单击按钮并且我想要查看时,我的列表将显示在对话框中 请检查用户指定的项目 单击,但它不工作。我的代码如下:Android 对话框中的setOnItemClickListener不工作,android,android-listview,Android,Android Listview,当用户单击按钮并且我想要查看时,我的列表将显示在对话框中 请检查用户指定的项目 单击,但它不工作。我的代码如下: private void showEditImageList() { final String names[] ={"A","B","C","D","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t"}; AlertDialog.Builder alertDialog = new AlertDi
private void showEditImageList() {
final String names[] ={"A","B","C","D","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t"};
AlertDialog.Builder alertDialog = new AlertDialog.Builder(SlotInformationActivity.this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.edit_image_list, null);
//convertView.setClickable(true);
//convertView.setOnClickListener(myClickListener);
alertDialog.setView(convertView);
alertDialog.setTitle("Tools");
ListView lv = (ListView) convertView.findViewById(R.id.editImageList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, names);
lv.setClickable(true);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("Click position:" + position);
}
});
alertDialog.show();
}
我认为这个代码会解决你的问题
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/editImageList"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
更新:
尝试从ListView中删除setClickable()。不确定,但可能在这里发生了事件截获,并且onItemClick不会触发。也可以代替System.out.println();使用Log.d(“我的应用程序”,“点击位置:+位置”);并选择调试选项在logcatOh我的天哪!!!这就是工作。非常感谢你。请您解释一下如何以及为什么?当您将其设置为“宽度”以包装内容时,列表将在字母结尾处进行包装,而不是全屏,请尝试运行您以前的代码并精确单击字母表,它们是可单击的,但其余区域只是空白
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/editImageList"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>