单击列表视图时如何显示警报对话框

时间:2012-09-18 04:44:32

标签: android listview alertdialog

当我点击我的活动列表视图时,我想显示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:线程退出未捕获异常(组= 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):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):在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):at java.lang.reflect.Method.invokeNative(Native Method)   09-18 11:52:09.679:E / AndroidRuntime(25866):at java.lang.reflect.Method.invoke(Method.java:507)   09-18 11:52:09.679:E / AndroidRuntime(25866):at 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):{“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“:”Road Bike“}]}

2 个答案:

答案 0 :(得分:8)

这可能有助于你

    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

答案 1 :(得分:-1)

这来自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();
    }

}

然后把它放在你想要调用Dialog的地方

ChangeLanguageDialog dialog = new ChangeLanguageDialog();
FragmentManager fm = getFragmentManager();
dialog.show(fm, "language");