我正在尝试获取列表视图以打开警报对话框,以便在用户单击选择时显示信息。我一直在谷歌开发者网站上尝试过几个与此有关但尚未得出结果的东西。这是我的代码。
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 :(得分: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();
答案 1 :(得分: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();
}
});
}
}