-1

当我点击我的活动的列表视图时,我想显示 alertdialog,我有一个显示 alertdialog 的代码,但我知道我的代码有问题,我不知道如何修复它,警报没有显示我想要,但在 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): FATAL EXCEPTION : 主要 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): at 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): at 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): at 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): { “结果”:1,"data":[{"0":"1","id_type":"1","1":"Cross Country (XC)","type_name":"Cross Country (XC)"},{"0 ":"2","id_type":"2","1":"BMX","type_name":"BMX"},{"0":"3","id_type":"3","1 ":"搭便车 (FR)","type_name":"搭便车 (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":"公路自行车"}]}

4

2 回答 2

9

这可能会帮助你

    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

于 2012-09-18T05:16:56.870 回答
-1

这是来自Android 开发人员
首先我创建了这个类,

@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");
于 2013-05-21T04:04:53.580 回答