由于 baseadapter 中的异步任务导致如何更新按钮文本
how to update button text due to asynctask result in baseadapter
由于 asynctext inner class 'follow' 或 'unfollow' 的结果,我想动态设置跟随按钮,但它不能正常工作。只有最少的按钮的文本发生变化。我需要一个解决方案。
这是我的 BaseAdapter class:
public class MainActivityLanguagesAdapter extends BaseAdapter {
Context c;
private List<MainActivityLanguagesModel> languages_list;
Button followButton;
public MainActivityLanguagesAdapter(List<MainActivityLanguagesModel> languages_list, Context c) {
this.languages_list = languages_list;
this.c = c;
}
@Override
public int getCount() {
return languages_list.size();
}
@Override
public Object getItem(int position) {
return languages_list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.cell_main_grid, parent, false);
}
followButton = (Button) convertView.findViewById(R.id.LanguagesCellFollowButton);
ImageView pictureView = (ImageView) convertView.findViewById(R.id.LanguagesCellPicture);
TextView title = (TextView) convertView.findViewById(R.id.LanguagesCellTitle);
Picasso.with(c).load(languages_list.get(position).getImage()).into(pictureView);
title.setText(languages_list.get(position).getTitle());
title.setTextColor(Color.WHITE);
followButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JSONObject following_Data = new JSONObject();
try {
following_Data.put("token", SessionInfo.login_informations.getToken());
following_Data.put("lang_id", languages_list.get(position).getId());
Log.d("lang_id", String.valueOf(languages_list.get(position).getId()));
} catch (JSONException e) {
e.printStackTrace();
}
new FollowingTask().execute(following_Data.toString());
}
});
return convertView;
}
class FollowingTask extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
}
public String doInBackground(String... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
ServerAddress.server_address + "languages/following");
httpPost.setHeader("content-type", "application/json");
StringEntity entity;
try {
entity = new StringEntity(params[0], HTTP.UTF_8);
httpPost.setEntity(entity);
} catch (Exception e) {
e.printStackTrace();
}
HttpResponse response;
final String petsResult;
try {
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
petsResult = EntityUtils.toString(responseEntity);
} catch (IOException e) {
e.printStackTrace();
return e.getMessage();
}
Log.d("petsResultString", petsResult.toString());
try {
JSONObject json = new JSONObject(petsResult);
int status = 0;
if (json.isNull("er") && json.getInt("e") == 0) {
JSONObject data = json.getJSONObject("d");
status = data.getInt("status");
}
return String.valueOf(status);
} catch (JSONException e) {
e.printStackTrace();
return e.getMessage();
}
}
public void onPostExecute(String result) {
Log.d("following_result", String.valueOf(result));
if (String.valueOf(result) == "0") {
followButton.setText("FOLLOW");
} else {
followButton.setText("UNFOLLOW");
}
notifyDataSetChanged();
}
}
}
当然只有一个按钮文本被改变了。您的变量 followButton 仅引用 Button 的一个实例。
如果您希望更改所有适配器对象,您可以为适配器 getView() 上的每个按钮添加一个布尔值并设置文本,如下所示:
...
boolean follow = false; // init with value you want
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
...
followButton = (Button) convertView.findViewById(R.id.LanguagesCellFollowButton);
if(follow){
followButton.setText("FOLLOW");
}else {
followButton.setText("UNFOLLOW");
}
...
}
...
public void onPostExecute(String result) {
...
if (String.valueOf(result) == "0") {
follow = true;
} else {
follow = false;
}
notifyDataSetChanged();
}
我是这样解决的。这是我的完整代码:
Context c;
private List<MainActivityLanguagesModel> languages_list;
int positionClass;
public MainActivityLanguagesAdapter(List<MainActivityLanguagesModel> languages_list, Context c) {
this.languages_list = languages_list;
this.c = c;
}
@Override
public int getCount() {
return languages_list.size();
}
@Override
public Object getItem(int position) {
return languages_list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.cell_main_grid, parent, false);
}
final Button followButton = (Button) convertView.findViewById(R.id.LanguagesCellFollowButton);
ImageView pictureView = (ImageView) convertView.findViewById(R.id.LanguagesCellPicture);
TextView title = (TextView) convertView.findViewById(R.id.LanguagesCellTitle);
Picasso.with(c).load(languages_list.get(position).getImage()).into(pictureView);
title.setText(languages_list.get(position).getTitle());
title.setTextColor(Color.WHITE);
if (languages_list.get(position).getIs_following() == 1)
followButton.setText("UNFOLLOW");
else
followButton.setText("FOLLOW");
followButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JSONObject following_Data = new JSONObject();
try {
positionClass = position;
Log.d("positionClass1", String.valueOf(positionClass));
following_Data.put("token", SessionInfo.login_informations.getToken());
following_Data.put("lang_id", languages_list.get(position).getId());
} catch (JSONException e) {
e.printStackTrace();
}
new FollowingTask().execute(following_Data.toString());
}
});
return convertView;
}
class FollowingTask extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
}
public String doInBackground(String... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
ServerAddress.server_address + "languages/following");
httpPost.setHeader("content-type", "application/json");
StringEntity entity;
try {
entity = new StringEntity(params[0], HTTP.UTF_8);
httpPost.setEntity(entity);
} catch (Exception e) {
e.printStackTrace();
}
HttpResponse response;
final String petsResult;
try {
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
petsResult = EntityUtils.toString(responseEntity);
} catch (IOException e) {
e.printStackTrace();
return e.getMessage();
}
Log.d("petsResultString", petsResult.toString());
try {
JSONObject json = new JSONObject(petsResult);
int status = 0;
if (json.isNull("er") && json.getInt("e") == 0) {
JSONObject data = json.getJSONObject("d");
status = data.getInt("status");
}
return String.valueOf(status);
} catch (JSONException e) {
e.printStackTrace();
return e.getMessage();
}
}
public void onPostExecute(String result) {
Log.d("isFollow", String.valueOf(result));
if (languages_list.get(positionClass).getIs_following() == 1)
languages_list.get(positionClass).setIs_following(0);
else
languages_list.get(positionClass).setIs_following(1);
notifyDataSetChanged();
}
}
由于 asynctext inner class 'follow' 或 'unfollow' 的结果,我想动态设置跟随按钮,但它不能正常工作。只有最少的按钮的文本发生变化。我需要一个解决方案。
这是我的 BaseAdapter class:
public class MainActivityLanguagesAdapter extends BaseAdapter {
Context c;
private List<MainActivityLanguagesModel> languages_list;
Button followButton;
public MainActivityLanguagesAdapter(List<MainActivityLanguagesModel> languages_list, Context c) {
this.languages_list = languages_list;
this.c = c;
}
@Override
public int getCount() {
return languages_list.size();
}
@Override
public Object getItem(int position) {
return languages_list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.cell_main_grid, parent, false);
}
followButton = (Button) convertView.findViewById(R.id.LanguagesCellFollowButton);
ImageView pictureView = (ImageView) convertView.findViewById(R.id.LanguagesCellPicture);
TextView title = (TextView) convertView.findViewById(R.id.LanguagesCellTitle);
Picasso.with(c).load(languages_list.get(position).getImage()).into(pictureView);
title.setText(languages_list.get(position).getTitle());
title.setTextColor(Color.WHITE);
followButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JSONObject following_Data = new JSONObject();
try {
following_Data.put("token", SessionInfo.login_informations.getToken());
following_Data.put("lang_id", languages_list.get(position).getId());
Log.d("lang_id", String.valueOf(languages_list.get(position).getId()));
} catch (JSONException e) {
e.printStackTrace();
}
new FollowingTask().execute(following_Data.toString());
}
});
return convertView;
}
class FollowingTask extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
}
public String doInBackground(String... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
ServerAddress.server_address + "languages/following");
httpPost.setHeader("content-type", "application/json");
StringEntity entity;
try {
entity = new StringEntity(params[0], HTTP.UTF_8);
httpPost.setEntity(entity);
} catch (Exception e) {
e.printStackTrace();
}
HttpResponse response;
final String petsResult;
try {
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
petsResult = EntityUtils.toString(responseEntity);
} catch (IOException e) {
e.printStackTrace();
return e.getMessage();
}
Log.d("petsResultString", petsResult.toString());
try {
JSONObject json = new JSONObject(petsResult);
int status = 0;
if (json.isNull("er") && json.getInt("e") == 0) {
JSONObject data = json.getJSONObject("d");
status = data.getInt("status");
}
return String.valueOf(status);
} catch (JSONException e) {
e.printStackTrace();
return e.getMessage();
}
}
public void onPostExecute(String result) {
Log.d("following_result", String.valueOf(result));
if (String.valueOf(result) == "0") {
followButton.setText("FOLLOW");
} else {
followButton.setText("UNFOLLOW");
}
notifyDataSetChanged();
}
}
}
当然只有一个按钮文本被改变了。您的变量 followButton 仅引用 Button 的一个实例。
如果您希望更改所有适配器对象,您可以为适配器 getView() 上的每个按钮添加一个布尔值并设置文本,如下所示:
...
boolean follow = false; // init with value you want
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
...
followButton = (Button) convertView.findViewById(R.id.LanguagesCellFollowButton);
if(follow){
followButton.setText("FOLLOW");
}else {
followButton.setText("UNFOLLOW");
}
...
}
...
public void onPostExecute(String result) {
...
if (String.valueOf(result) == "0") {
follow = true;
} else {
follow = false;
}
notifyDataSetChanged();
}
我是这样解决的。这是我的完整代码:
Context c;
private List<MainActivityLanguagesModel> languages_list;
int positionClass;
public MainActivityLanguagesAdapter(List<MainActivityLanguagesModel> languages_list, Context c) {
this.languages_list = languages_list;
this.c = c;
}
@Override
public int getCount() {
return languages_list.size();
}
@Override
public Object getItem(int position) {
return languages_list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.cell_main_grid, parent, false);
}
final Button followButton = (Button) convertView.findViewById(R.id.LanguagesCellFollowButton);
ImageView pictureView = (ImageView) convertView.findViewById(R.id.LanguagesCellPicture);
TextView title = (TextView) convertView.findViewById(R.id.LanguagesCellTitle);
Picasso.with(c).load(languages_list.get(position).getImage()).into(pictureView);
title.setText(languages_list.get(position).getTitle());
title.setTextColor(Color.WHITE);
if (languages_list.get(position).getIs_following() == 1)
followButton.setText("UNFOLLOW");
else
followButton.setText("FOLLOW");
followButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JSONObject following_Data = new JSONObject();
try {
positionClass = position;
Log.d("positionClass1", String.valueOf(positionClass));
following_Data.put("token", SessionInfo.login_informations.getToken());
following_Data.put("lang_id", languages_list.get(position).getId());
} catch (JSONException e) {
e.printStackTrace();
}
new FollowingTask().execute(following_Data.toString());
}
});
return convertView;
}
class FollowingTask extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
}
public String doInBackground(String... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
ServerAddress.server_address + "languages/following");
httpPost.setHeader("content-type", "application/json");
StringEntity entity;
try {
entity = new StringEntity(params[0], HTTP.UTF_8);
httpPost.setEntity(entity);
} catch (Exception e) {
e.printStackTrace();
}
HttpResponse response;
final String petsResult;
try {
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
petsResult = EntityUtils.toString(responseEntity);
} catch (IOException e) {
e.printStackTrace();
return e.getMessage();
}
Log.d("petsResultString", petsResult.toString());
try {
JSONObject json = new JSONObject(petsResult);
int status = 0;
if (json.isNull("er") && json.getInt("e") == 0) {
JSONObject data = json.getJSONObject("d");
status = data.getInt("status");
}
return String.valueOf(status);
} catch (JSONException e) {
e.printStackTrace();
return e.getMessage();
}
}
public void onPostExecute(String result) {
Log.d("isFollow", String.valueOf(result));
if (languages_list.get(positionClass).getIs_following() == 1)
languages_list.get(positionClass).setIs_following(0);
else
languages_list.get(positionClass).setIs_following(1);
notifyDataSetChanged();
}
}