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);
}
});
}
}
即時通訊沒有得到任何對話。我仍然無法弄清楚這是如何工作的,因爲它看起來像我應該工作,但沒有。你有更多的想法嗎? – 2010-08-14 23:33:02
上面的代碼工作得很好。代碼中缺少的可能是show()方法。檢查一個簡單的家庭活動,你會看到對話框出現。 – Pentium10 2010-08-15 08:31:41