0

我有以下代码......这个列表基本上是“监听”要单击的项目,一旦单击,它会打开一个对话框,里面有一个按钮。我无法让按钮听...即使我这样做...应用程序也会崩溃。并且 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) {

                        }
                      });


              }
4

3 回答 3

0

抓住了错误

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

按钮 postbutton=(按钮) dialog.findViewById(R.id.buttonfb); 如果按钮在对话框内。

于 2012-11-30T04:32:46.240 回答
0

-我认为postbutton是在里面dialog,如果是这样你需要id通过以下方式找到它......

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

于 2012-11-30T04:38:08.830 回答
0

用这个替换你的代码..

因为您的按钮在对话框内,所以您需要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) {

                    }
                  });


          }
于 2012-11-30T05:23:42.027 回答