单击列表视图时如何显示警报对话框
Posted
技术标签:
【中文标题】单击列表视图时如何显示警报对话框【英文标题】:How to show Alert Dialog when click on listview 【发布时间】:2012-09-10 07:59:54 【问题描述】:我想在单击我的活动的列表视图时显示警报对话框,我有一个显示警报对话框的代码,但我知道我的代码有问题,我不知道如何修复它,警报没有显示我想要的,但在 DDMS 中得到我想要的结果。这是我的代码:
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
new bike_type_dao(responsetype).view_bike_type();
final Entity_Brand brand = adapterBrand.getItem(position);
LayoutInflater li = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = li.inflate(R.layout.listview_activity, null, false);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(brand.getBrand_name());
builder.setView(view);
ListView listtype = (ListView) view.findViewById(R.id.list_sepeda);
listtype.setOnClickListener((OnClickListener) this);
listtype.setAdapter(new ArrayAdapter<Entity_Bike_Type>(this,
android.R.layout.simple_list_item_1, arrayList));
listtype.setOnItemClickListener((OnItemClickListener) this);
builder.create().show();
谢谢。
那是我的日志猫:
09-18 11:52:07.819: E/HttpResponse(25866): "result":1,"data":["0":"1","id_brand":"1","1 ":"Polygon","brand_name":"Polygon","0":"2","id_brand":"2","1":"United Bike","brand_name":"United Bike" ,"0":"3","id_brand":"3","1":"WimCycle","brand_name":"WimCycle"] 09-18 11:52:09.669: D/AndroidRuntime(25866): 关闭 VM 09-18 11:52:09.669: W/dalvikvm(25866): threadid=1: 线程以未捕获的异常退出 (group=0x40018578) 09-18 11:52:09.679:E/AndroidRuntime(25866):致命异常:主要 09-18 11:52:09.679: E/AndroidRuntime(25866): java.lang.ClassCastException: com.wilis.hellotabwidget.Tab_Brand_ListView_Activity 09-18 11:52:09.679: E/AndroidRuntime(25866): 在 com.wilis.hellotabwidget.Tab_Brand_ListView_Activity.onListItemClick(Tab_Brand_ListView_Activity.java:158) 09-18 11:52:09.679: E/AndroidRuntime(25866): 在 android.app.ListActivity$2.onItemClick(ListActivity.java:319) 09-18 11:52:09.679: E/AndroidRuntime(25866): 在 android.widget.AdapterView.performItemClick(AdapterView.java:284) 09-18 11:52:09.679: E/AndroidRuntime(25866): 在 android.widget.ListView.performItemClick(ListView.java:3755) 09-18 11:52:09.679: E/AndroidRuntime(25866): 在 android.widget.AbsListView$PerformClick.run(AbsListView.java:1964) 09-18 11:52:09.679: E/AndroidRuntime(25866): 在 android.os.Handler.handleCallback(Handler.java:587) 09-18 11:52:09.679: E/AndroidRuntime(25866): 在 android.os.Handler.dispatchMessage(Handler.java:92) 09-18 11:52:09.679: E/AndroidRuntime(25866): 在 android.os.Looper.loop(Looper.java:130) 09-18 11:52:09.679: E/AndroidRuntime(25866): 在 android.app.ActivityThread.main(ActivityThread.java:3687) 09-18 11:52:09.679: E/AndroidRuntime(25866): 在 java.lang.reflect.Method.invokeNative(Native Method) 09-18 11:52:09.679: E/AndroidRuntime(25866): 在 java.lang.reflect.Method.invoke(Method.java:507) 09-18 11:52:09.679: E/AndroidRuntime(25866): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 09-18 11:52:09.679: E/AndroidRuntime(25866): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 09-18 11:52:09.679: E/AndroidRuntime(25866): at dalvik.system.NativeStart.main(Native Method) 09-18 11:52:09.809: E/HttpResponse(25866): "result":1,"data":["0":"1","id_type":"1","1":"越野 (XC)","type_name":"越野 (XC)","0":"2","id_type":"2","1":"BMX","type_name":" BMX","0":"3","id_type":"3","1":"Free Ride (FR)","type_name":"Free Ride (FR)","0" :"4","id_type":"4","1":"DownHill (DH)","type_name":"DownHill (DH)","0":"5","id_type":" 5","1":"DirtJump (DJ)","type_name":"DirtJump (DJ)","0":"6","id_type":"6","1":"公路自行车","type_name":"公路自行车"]
【问题讨论】:
你的活动中第 158 行的内容。请在此处粘贴该列表 listtype.setAdapter(new ArrayAdapter这可能对你有帮助
listview.setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long rowId)
AlertDialog.Builder adb = new AlertDialog.Builder(
MainActivity.this);
adb.setTitle("List");
adb.setMessage(" selected Item is="
+parent.getItemAtPosition(position));
adb.setPositiveButton("Ok", null);
adb.show();
);
这里用你的类名更改 MainActivity.this
【讨论】:
只需使用 setOnItemClickListener 和 alert Dialog 作为提及【参考方案2】:这是来自Android Developers: 首先我创建了这个类,
@SuppressLint("NewApi")
public class ChangeLanguageDialog extends DialogFragment
public Dialog onCreateDialog(Bundle savedInstanceState)
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.app_name)
.setPositiveButton(R.string.save, new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int id)
)
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int id)
);
return builder.create();
然后把它放在你想调用对话框的地方
ChangeLanguageDialog dialog = new ChangeLanguageDialog();
FragmentManager fm = getFragmentManager();
dialog.show(fm, "language");
【讨论】:
以上是关于单击列表视图时如何显示警报对话框的主要内容,如果未能解决你的问题,请参考以下文章