2011-10-14 21 views
10

我在佈局中使用帶有自定義適配器的列表視圖。 現在我試圖將包含列表的佈局帶到alertdialog。 我試圖把不包含列表的簡單佈局用此代碼提醒對話框,它的工作良好。 但我無法將包含佈局的列表放入alertdialog。如何在警報對話框中擴展包含listview的佈局?

  AlertDialog.Builder dialog = new AlertDialog.Builder(this); 
      dialog.setView(getLayoutInflater().inflate(R.layout.smill, null)); 
      dialog.setIcon(R.drawable.androidsmile); 
      dialog.setInverseBackgroundForced(true); 


      dialog.setTitle("Select smiley"); 
      dialog.setPositiveButton("Cancel", null); 
      dialog.show(); 
+1

你能否詳細說明什麼問題與您的應用程序發生了什麼? 它強制關閉或listview沒有顯示或其他任何東西。 –

回答

14

您所做的只是將視圖擴大到警報對話框。你沒有在該列表視圖上設置適配器,所以當然它不會工作(因爲它是空的)。

你需要做的是這樣的:

View view = getLayoutInflater().inflate(R.layout.smill, null) 
ListView listView = (ListView) view.findViewById(R.id.listView); 
YourCustomAdapter adapter = new YourCustomAdapter(parameters...); 
listView.setAdapter(adapter); 

AlertDialog.Builder dialog = new AlertDialog.Builder(this); 
dialog.setView(view); 
... 
... 
dialog.show(); 
+2

避免將null作爲layoutInflater的根視圖傳遞。如果這是在適配器內完成的,則可以傳遞rootview或convertview或任何視圖託管此警報對話框 – Odaym