我的ListView包含一个包含Button等的项目。我从ListView的Adapter类中启动Asynctask:
holder.btn_follow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Follow_Async().execute(arr_id.get(position), arr_name.get(position));
}
}
});
这是我的Asynctask:
public class Follow_Async extends AsyncTask<String, Void, String> {
@Override
protected void onPostExecute(String result) {
//here
Toast.makeText(Activity_Followers2.this, "You are now following " + result, Toast.LENGTH_SHORT).show();
}
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
try {
params_follow = new ArrayList<NameValuePair>();
params_follow.add(new BasicNameValuePair("session_userid", session_userid));
params_follow.add(new BasicNameValuePair("friend_userid", params[0]));
JSONParser jsonParser; jsonParser = new JSONParser();
JSONObject json = jsonParser.makeHttpRequest(add_follow, "POST", params_follow);
} catch (Exception e) {
e.printStackTrace();
}
return params[1];
}
}
在PostExecute()中,我想更改我刚刚点击取消关注的按钮的文本。我怎样才能从那里访问它?
答案 0 :(得分:1)
只需将按钮作为参数传递给AsyncTask构造函数:
在适配器中:
final thisButton = holder.btn_follow; // <------------ add a final Button field
thisButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Follow_Async(thisButton). //<------------- pass button to AsyncTask
execute(arr_id.get(position), arr_name.get(position));
}
}
});
在AsyncTask中:
class Follow_Async extends AsyncTask<String, Void, String> {
private final Button button;
public Follow_Async(Button button) { //<------------- pass button to constructor
this.button = button;
}
@Override
protected void onPostExecute(String result) {
this.button.setText(""); //<--------------- do something with button
}
}