【问题标题】:how to update button text due to asynctask result in baseadapter [closed]由于baseadapter中的asynctask导致如何更新按钮文本[关闭]
【发布时间】:2016-08-10 15:25:20
【问题描述】:

由于异步文本内部类“关注”或“取消关注”的结果,我想动态设置关注按钮,但它不能清楚地工作。只有最少按钮的文本发生变化。我需要一个解决方案。

这是我的 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();

    }

}

}

【问题讨论】:

  • 在什么情况下不起作用?您是否收到特定的错误消息?

标签: android android-asynctask baseadapter


【解决方案1】:

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

    }

【讨论】:

    【解决方案2】:

    我是这样解决的。这是我的完整代码:

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

    【讨论】:

      猜你喜欢
      • 2020-10-20
      • 1970-01-01
      • 2012-11-30
      • 1970-01-01
      • 1970-01-01
      • 2020-11-01
      • 1970-01-01
      • 2011-03-03
      • 1970-01-01
      相关资源
      最近更新 更多