2010-08-13 150 views
0

我想讓我的列表視圖打開一個警告對話框,以便在用戶點擊選擇時顯示信息。我一直在嘗試Google Developers網站上的幾件與此有關的事情,但尚未得出結果。這是我的代碼。ListView打開一個對話框(或提示對話框)

package table.periodic; 

import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.AdapterView.OnItemClickListener; 

public class Anions extends Activity {  AlertDialog.Builder builder; AlertDialog alertDialog;  String[] anions ={   "Acetate", "Bromide", "Carbonate", "Chlorate", "Chloride", "Chlorite", "Chromate",    "Cyanide", "Dichromate", "Fluoride", "Hexacyanoferrate(II)", "Hexacyanoferrate(III)",   "Hydride", "Hydrogen Carbonate", "Hydrogen Sulfate", "Hydroxide", "Hypochlorite",   "Iodide", "Nitrate", "Nitrite", "Oxide", "Perchlorate", "Permanganate", "Peroxide",    "Phosphate", "Sulfate", "Sulfide", "Sulfite" };  public void onCreate(Bundle icicle){  super.onCreate(icicle);   setContentView(R.layout.anions); 
       ListView list=(ListView)findViewById(R.id.anionlist); 
     list.setTextFilterEnabled(true); 

     ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, anions); 

     list.setAdapter(aa); 

     list.setOnItemClickListener(new OnItemClickListener() { 

      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
         String itemText = anions[arg2]; 

     new AlertDialog.Builder(Anions.this); 
      Context mContext = getApplicationContext(); 
      Dialog dialog = new Dialog(mContext); 
       dialog.setContentView(R.layout.custom_dialog); 
       dialog.setTitle(itemText); 

       ImageView image = (ImageView) dialog.findViewById(R.id.image); 
       image.setImageResource(R.drawable.hydrogen); 



     } 

     }); 
      } 

    } 

回答

0

而不是getApplicationContext()用你的活動作爲上下文Anions.this

不要忘記調用.create().show()

例如,這對我的作品:

下面的代碼
new AlertDialog.Builder(this).setTitle(R.string.contact_groups_add) 
      .setView(addView).setPositiveButton(R.string.ok, 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, 
           int whichButton) { 

         } 
        }).setNegativeButton(R.string.cancel, 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, 
           int whichButton) { 

         } 
        }).show(); 
+0

即時通訊沒有得到任何對話。我仍然無法弄清楚這是如何工作的,因爲它看起來像我應該工作,但沒有。你有更多的想法嗎? – 2010-08-14 23:33:02

+0

上面的代碼工作得很好。代碼中缺少的可能是show()方法。檢查一個簡單的家庭活動,你會看到對話框出現。 – Pentium10 2010-08-15 08:31:41

0

喜正在爲我工​​作

package com.example.slide; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.ListView; 

public class MainActivity extends Activity {  
    AlertDialog.Builder builder; 
    AlertDialog alertDialog;  
    String[] anions ={"Acetate", "Bromide", "Carbonate", "Chlorate", "Chloride", "Chlorite", "Chromate" };  
    public void onCreate(Bundle icicle){ 
     super.onCreate(icicle);  
     setContentView(R.layout.activity_main); 
     ListView list=(ListView)findViewById(R.id.listView); 
     list.setTextFilterEnabled(true); 

     ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, anions); 
     list.setAdapter(aa); 
     list.setOnItemClickListener(new OnItemClickListener() { 

      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { 
       String itemText = anions[arg2]; 

       //new AlertDialog.Builder(MainActivity.this); 
       //Context mContext = getApplicationContext(); 
       Dialog dialog = new Dialog(MainActivity.this); 
       dialog.setContentView(R.layout.row); 
       dialog.setTitle(itemText); 

       ImageView image = (ImageView) dialog.findViewById(R.id.imageView); 
       image.setImageResource(R.drawable.ic_launcher); 
       dialog.show(); 
      } 
     }); 
    } 
}