2015-07-19 165 views
2

我的名單將在對話框中顯示,當用戶點擊按鈕,我想 檢查用戶setOnItemClickListener對話框不工作

點擊,但它不工作的項目。我的代碼如下:

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> 
+0

嘗試從刪除setClickable()你列表顯示。不確定,但也許攔截事件發生在這裏onItemClick不會觸發。 – Viacheslav

+0

而不是System.out.println();使用Log.d(「MY_APP」,「Click position:」+ position);並選擇logcat中的調試選項 –

回答

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

+0

哦,我的天哪!這是工作。非常感謝。你介意給我解釋一下如何和爲什麼,請問? – barssala

+1

那麼當你將它設置爲wrap_content的寬度時,該列表將它封裝在信件結尾而不是屏幕全寬的地方,嘗試運行你以前的代碼,並且完全點擊它們可點擊的字母,但是休息區域只是空白 –