我有以下代码......这个列表基本上是“监听”要点击的项目,一旦点击,它会打开一个带有按钮的对话框。 我无法让按钮听...即使我这样做......应用程序也会崩溃。 并且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) {
}
});
}
答案 0 :(得分:0)
抓住了错误
按钮postbutton =(按钮)findViewById(R.id.buttonfb);应该是
按钮postbutton =(按钮)dialog.findViewById(R.id.buttonfb);如果按钮在对话框内。
答案 1 :(得分:0)
- 我认为postbutton
位于dialog
内,如果是,那么您需要按以下方式找到id
.... ..
Button postbutton=(Button)dialog.findViewById(R.id.buttonfb);
答案 2 :(得分: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) {
}
});
}