我在为多个按钮实现一个OnClickListener
时遇到问题。这些按钮是根据AsyncHttpClient
的OnSuccess中的响应动态生成的。
如果我为每个按钮写OnClickListener
,一切正常。但是当我实现OnClickListener
并覆盖onClick(View v)
并在OnSuccess(AsyncHttpClient)中调用b1.setOnClickListener(this);
时,我收到错误 - 方法setOnClickListener(View.OnClickListener)在类型View中不适用于参数(new AsyncHttpResponseHandler(){})
以下是我的代码结构
public class USFragment extends Fragment implements OnClickListener {
public void getUSData(String loc, String bc, String bm){
AsyncHttpClient client = new AsyncHttpClient();
client.get("url",reqparams ,new AsyncHttpResponseHandler() {
public void onSuccess(String response) {
//getting arraylist from jason response
if(count == 2)
{
Button b1 = new Button(getActivity());
b1.setText("Click for Suc Uplds");
b1.setId(count);
b1.setOnClickListener(this); //getting error here
// below commented code works, but i dont want to write onclickListener for each button.
/*b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(getActivity(), UploadOnClickDetailsActivity.class);
myIntent.putExtra("detailType", v.getId() );
startActivity(myIntent);
}
});*/
tr1.addView(b1);
}
}
}
}
@Override
public void onClick(View v) {
//depend on button id or v.getId, I am starting another activity
Intent myIntent = new Intent(getActivity(), UploadOnClickDetailsActivity.class);
myIntent.putExtra("detailType", v.getId() );
startActivity(myIntent);
}
}
感谢您的帮助。感谢