barssala
barssala

Reputation: 483

setOnItemClickListener in dialog is not working

My list will show in the dialog when user click the button and I want to check the item that user

click but it's not working. My code is as following:

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

}

Please help and thank you.

For the code in file 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>

Upvotes: 2

Views: 1521

Answers (1)

Sergey Zabelnikov
Sergey Zabelnikov

Reputation: 1955

I think that this code will solve your problem

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

update: enter image description here

Upvotes: 2

Related Questions