无法在自定义对话框中添加数组列表

时间:2014-03-11 08:08:25

标签: android dialog customdialog

我想在自定义对话框中添加数组列表,但它在数组适配器(R.layout.simple_list_item_1)中显示错误,我也改为R.layout.simple_list_item_1,但它不起作用。我在下面发布了我的代码。

dobtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                docustomdiaog();

            }

            private void docustomdiaog() 
            {
                // TODO Auto-generated method stub
                final Dialog dialog=new Dialog(Doyouwant.this);

                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

                dialog.setContentView(R.layout.dodialog);

                listdata=(ListView)dialog.findViewById(R.id.dclistView1);

                dsavebtn=(Button)dialog.findViewById(R.id.dcsave);

                dokbtn=(Button)dialog.findViewById(R.id.dcok);



                arylistdata=new ArrayList<String>();

                adpater=new ArrayAdapter<String>(this,R.layout.simple_list_item_1, arylistdata);

                dsavebtn.setOnClickListener(new OnClickListener() {


                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        addtolist=customtxt.getText().toString();

                        arylistdata.add(addtolist);

                        listdata.setAdapter(adpater);


                        dialog.dismiss();
                    }
                });

                dokbtn.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });
                dialog.show();
            }

        });

2 个答案:

答案 0 :(得分:0)

更改此

adpater=new ArrayAdapter<String>(this,R.layout.simple_list_item_1, arylistdata);

作为

adpater=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, arylistdata);

答案 1 :(得分:0)

docustomdiaog()方法放在OnClickListener()之外,如下所示......

dobtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            docustomdiaog();

        }



    });


 private void docustomdiaog() 
        {
            // TODO Auto-generated method stub
            final Dialog dialog=new Dialog(Doyouwant.this);

            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

            dialog.setContentView(R.layout.dodialog);

            listdata=(ListView)dialog.findViewById(R.id.dclistView1);

            dsavebtn=(Button)dialog.findViewById(R.id.dcsave);

            dokbtn=(Button)dialog.findViewById(R.id.dcok);



            arylistdata=new ArrayList<String>();

            adpater=new ArrayAdapter<String>(this,R.layout.simple_list_item_1, arylistdata);

            dsavebtn.setOnClickListener(new OnClickListener() {


                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    addtolist=customtxt.getText().toString();

                    arylistdata.add(addtolist);

                    listdata.setAdapter(adpater);


                    dialog.dismiss();
                }
            });

            dokbtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            });
            dialog.show();
        }