对话框中的setOnItemClickListener不起作用

时间:2015-07-19 06:29:56

标签: 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 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();

}

请帮助,谢谢。

对于文件edit_image_list.xml

中的代码
<?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="wrap_content"
    android:layout_height="fill_parent"
    />

</LinearLayout>

1 个答案:

答案 0 :(得分:2)

我认为此代码可以解决您的问题

<?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>

<强>更新 enter image description here