gpt4 book ai didi

android - 从外部访问自定义 ListView 对象

转载 作者:行者123 更新时间:2023-11-30 02:47:28 24 4
gpt4 key购买 nike

我有一个 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));
}

}
});

这是我的异步任务:

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() 中,我想将刚刚单击的按钮的文本更改为取消关注。我如何从那里访问它?

最佳答案

只需将您的按钮作为参数传递给 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
}

}

关于android - 从外部访问自定义 ListView 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24790325/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com