如何更新按钮文本由于asynctask导致baseadapter

时间:2016-08-10 15:25:20

标签: android android-asynctask baseadapter

由于asynctext内部类'follow'或'unfollow'的结果,我想动态设置跟随按钮,但它不能正常工作。只有最小按钮的文本会发生变化。我需要一个解决方案。

这是我的BaseAdapter类:

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();

    }

}

}

2 个答案:

答案 0 :(得分:0)

当然只有一个按钮文字被更改。您的变量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();

    }

答案 1 :(得分:0)

我解决了这个问题。这是我的完整代码:

build.gradle