Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何更新baseadapter中由于asynctask导致的按钮文本_Android_Android Asynctask_Baseadapter - Fatal编程技术网

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

Android 如何更新baseadapter中由于asynctask导致的按钮文本,android,android-asynctask,baseadapter,Android,Android Asynctask,Baseadapter,由于asynctext内部类“follow”或“unfollow”的结果,我想动态设置follow按钮,但它不能很清楚地工作。只有最小按钮的文本更改。我需要一个解决方案 下面是我的BaseAdapter类: public class MainActivityLanguagesAdapter extends BaseAdapter { Context c; private List<MainActivityLanguagesModel> languages_list

由于asynctext内部类“follow”或“unfollow”的结果,我想动态设置follow按钮,但它不能很清楚地工作。只有最小按钮的文本更改。我需要一个解决方案

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

    }

}
public类MainActivityLanguagesAdapter扩展BaseAdapter{
上下文c;
私人名单;;
按钮跟随按钮;
公共维护活动语言适配器(列表语言\u列表,上下文c){
this.languages\u list=语言\u list;
这个.c=c;
}
@凌驾
public int getCount(){
返回语言_list.size();
}
@凌驾
公共对象getItem(int位置){
返回语言列表。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
LayoutInflater充气机=(LayoutInflater)c
.getSystemService(上下文布局\充气机\服务);
if(convertView==null){
convertView=充气机。充气(R.layout.cell\u主网格,父网格,false);
}
followButton=(Button)convertView.findViewById(R.id.LanguagesCellFollowButton);
ImageView pictureView=(ImageView)convertView.findViewById(R.id.LanguagesCellPicture);
TextView title=(TextView)convertView.findViewById(R.id.LanguagesCellTitle);
毕加索.with(c).load(语言)_list.get(位置).getImage())到(pictureView);
title.setText(languages_list.get(position.getTitle());
title.setTextColor(Color.WHITE);
followButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
数据后面的JSONObject=新JSONObject();
试一试{
以下是_Data.put(“token”,SessionInfo.login_informations.getToken());
以下是_Data.put(“lang_id”,languages_list.get(position.getId());
Log.d(“lang_id”,String.valueOf(languages_list.get(position.getId()));
}捕获(JSONException e){
e、 printStackTrace();
}
新建followTask().execute(following_Data.toString());
}
});
返回视图;
}
类followTask扩展了AsyncTask{
受保护的void onPreExecute(){
super.onPreExecute();
}
公共字符串doInBackground(字符串…参数){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(
ServerAddress.server_地址+“语言/以下”);
setHeader(“内容类型”、“应用程序/json”);
实体;
试一试{
实体=新的StringEntity(参数[0],HTTP.UTF_8);
httpPost.setEntity(实体);
}捕获(例外e){
e、 printStackTrace();
}
HttpResponse响应;
最终字符串petsResult;
试一试{
response=httpClient.execute(httpPost);
HttpEntity responseEntity=response.getEntity();
petsResult=EntityUtils.toString(responseEntity);
}捕获(IOE异常){
e、 printStackTrace();
返回e.getMessage();
}
Log.d(“petsResultString”,petsResult.toString());
试一试{
JSONObject json=新的JSONObject(petsResult);
int status=0;
if(json.isNull(“er”)&&json.getInt(“e”)==0){
JSONObject data=json.getJSONObject(“d”);
status=data.getInt(“status”);
}
返回字符串.valueOf(状态);
}捕获(JSONException e){
e、 printStackTrace();
返回e.getMessage();
}
}
public void onPostExecute(字符串结果){
Log.d(“跟随_结果”,String.valueOf(结果));
if(字符串值of(结果)=“0”){
followButton.setText(“FOLLOW”);
}否则{
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();

    }

}
Context c;
私人名单;;
国际职业分类;
公共维护活动语言适配器(列表语言\u列表,上下文c){
this.languages\u list=语言\u list;
这个.c=c;
}
@凌驾
public int getCount(){
返回语言_list.size();
}
@凌驾
公共对象getItem(int位置){
返回语言列表。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
LayoutInflater充气机=(LayoutInflater)c
.getSystemService(上下文布局\充气机\服务);
if(convertView==null){
convertView=充气机。充气(R.layout.cell\u主网格,父网格,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);
毕加索.with(c).load(语言)_list.get(位置).getImage())到(pictureView);
title.setText(languages_list.get(position.getTitle());
title.setTextColor(Color.WHITE);
if(语言列表.get(位置).getIs\u following()==1)
followButton.setText(“UNFOLLOW”);
其他的
followButton.setText(“FOLLOW”);
followButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
数据后面的JSONObject=新JSONObject();
试一试{
职位类别=职位;
Log.d(“位置类别1”,字符串