简体   繁体   English

Android中的侦听器内的侦听器按钮

[英]Listener button inside a listener in Android

I have the following code... and this list is basically "listening" for an item to be clicked and once clicked, it opens a Dialog box with a button inside. 我有以下代码...该列表基本上是“侦听”要单击的项目,一旦单击,它将打开一个对话框,其中包含一个按钮。 I am not able to make the button listen ... Even if i do...the app will crash. 我无法让按钮监听...即使我...该应用程序也会崩溃。 And eclipse is not automatically allowing me to "over-ride" the onClick() 而且eclipse不会自动允许我“重写” onClick()

WeatherAdapter adapter = new WeatherAdapter(this,R.layout.listview_header_row, weather_data);
            listView1 = (ListView)findViewById(R.id.listView1);
            View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
            //listView1.addHeaderView(header);
            listView1.setAdapter(adapter);  
            listView1.setClickable(true);
            listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {

              @Override
              public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

                    Dialog  dialog = new Dialog (Activity2.this);
                    dialog.setContentView(R.layout.dialogxml);
                    dialog.setCancelable(true);
                    ImageView dcover=(ImageView) dialog.findViewById(R.id.dimageView1);
                    TextView dtitle = (TextView) dialog.findViewById(R.id.dtextView1);
                    TextView dyear = (TextView) dialog.findViewById(R.id.dtextView2);
                    TextView ddirector = (TextView) dialog.findViewById(R.id.dtextView3);
                    TextView drating = (TextView) dialog.findViewById(R.id.dtextView4);
                    TextView len1 = (TextView) dialog.findViewById(R.id.textView77);
                    Button postbutton=(Button) findViewById(R.id.buttonfb);

                    InputStream is;
                    try {
                        is = (InputStream) new URL(full_data[position][0]).getContent();
                        Drawable dd = Drawable.createFromStream(is, "src name");
                        dcover.setImageDrawable(dd);
                    } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }


                    dtitle.setText("Name: "+full_data[position][1]);
                    dyear.setText("Year: "+full_data[position][2]);
                    ddirector.setText("Director: "+full_data[position][4]);
                    drating.setText("Rating: "+full_data[position][3]+"/10");
                    dialog.show();
          //NOT ABLE TO DO THIS!!          postbutton.setOnClickListener(new View.OnClickListener() { 
                        @Override
                        public void onClick(View v) {

                        }
                      });


              }

Caught the error 抓到错误

Button postbutton=(Button) findViewById(R.id.buttonfb); 按钮postbutton =(Button)findViewById(R.id.buttonfb); should be 应该

Button postbutton=(Button) dialog.findViewById(R.id.buttonfb); 按钮postbutton =(按钮)dialog.findViewById(R.id.buttonfb); if the button is inside the dialog. 如果按钮在对话框中。

-我认为postbuttondialog ,如果是这样,则您需要通过以下方式找到其id ...

Button postbutton=(Button)dialog.findViewById(R.id.buttonfb);

Replace your code with this.. 以此替换您的代码。

because your button is inside dialog so you need to findviewbyid using dialog view. 因为您的按钮在对话框内,所以您需要使用对话框视图来findviewbyid

WeatherAdapter adapter = new WeatherAdapter(this,R.layout.listview_header_row, weather_data);
        listView1 = (ListView)findViewById(R.id.listView1);
        View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
        //listView1.addHeaderView(header);
        listView1.setAdapter(adapter);  
        listView1.setClickable(true);
        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

                Dialog  dialog = new Dialog (Activity2.this);
                dialog.setContentView(R.layout.dialogxml);
                dialog.setCancelable(true);
                ImageView dcover=(ImageView) dialog.findViewById(R.id.dimageView1);
                TextView dtitle = (TextView) dialog.findViewById(R.id.dtextView1);
                TextView dyear = (TextView) dialog.findViewById(R.id.dtextView2);
                TextView ddirector = (TextView) dialog.findViewById(R.id.dtextView3);
                TextView drating = (TextView) dialog.findViewById(R.id.dtextView4);
                TextView len1 = (TextView) dialog.findViewById(R.id.textView77);
                Button postbutton=(Button) dialog.findViewById(R.id.buttonfb);

                InputStream is;
                try {
                    is = (InputStream) new URL(full_data[position][0]).getContent();
                    Drawable dd = Drawable.createFromStream(is, "src name");
                    dcover.setImageDrawable(dd);
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                dtitle.setText("Name: "+full_data[position][1]);
                dyear.setText("Year: "+full_data[position][2]);
                ddirector.setText("Director: "+full_data[position][4]);
                drating.setText("Rating: "+full_data[position][3]+"/10");
                dialog.show();
            postbutton.setOnClickListener(new View.OnClickListener() { 
                    @Override
                    public void onClick(View v) {

                    }
                  });


          }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM