当前位置: 动力学知识库 > 问答 > 编程问答 >

java - 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()

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) {

}

});

}

网友答案:

Replace your code with this..

because your button is inside dialog so you need to findviewbyid using dialog view.

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) {

                    }
                  });


          }
网友答案:

Caught the error

Button postbutton=(Button) findViewById(R.id.buttonfb); should be

Button postbutton=(Button) dialog.findViewById(R.id.buttonfb); if the button is inside the dialog.

网友答案:

- I think the postbutton is inside the dialog, and if its so you need to find its id in the following way......

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: