notifyDataSetChanged()不从listview中删除列表项

时间:2015-02-19 16:19:21

标签: android listview android-listview

我正在尝试从使用CustomAdapter填充的Listview中删除列表项作为单击的按钮。我的自定义适配器的getView()方法如下所示:

public View getView(final int position, View convertView, ViewGroup parent) {
    final NonFollowersHolder holder;
    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.listitem_nonfollowers, parent, false);

        holder = new NonFollowersHolder();
        holder.tv_fullname = (TextView)convertView.findViewById(R.id.tv_fullname);
        holder.tv_username = (TextView)convertView.findViewById(R.id.tv_username);
        holder.tv_bio = (TextView)convertView.findViewById(R.id.tv_bio);
        holder.userpic = (NetworkImageView)convertView.findViewById(R.id.iv_userpic);
        holder.action = (ImageView)convertView.findViewById(R.id.action);

        convertView.setTag(holder);
    }
    else{
        holder = (NonFollowersHolder)convertView.getTag();
    }
    holder.tv_fullname.setText(list.get(position).fullname);
    holder.tv_username.setText(list.get(position).username);
    String temp = list.get(position).bio;
    holder.tv_bio.setText(temp);
    holder.userpic.setImageUrl(list.get(position).profilepic, loader);

    if(isUnfollow == true) holder.action.setImageDrawable(context.getResources().getDrawable(R.drawable.bt_unfollow));
    else holder.action.setImageDrawable(context.getResources().getDrawable(R.drawable.bt_follow));

    holder.action.setOnClickListener(new MyListener(position));
    return convertView;
}

并且clickListener类MyListener为:

private class MyListener implements View.OnClickListener {

    private int position;
    public MyListener(int position) {
        this.position = position;
    }

    @Override
    public void onClick(View v) {
        String url = AppProperties.APIURL+"/users/"+list.get(position).id
                +"/relationship?access_token="+session.getValue(AppProperties.ACCESS_TOKEN);
        if(isUnfollow){

            RequestParams params = new RequestParams();
            params.put("action", "unfollow");
            InstagramRestClient.post(url, params, new JsonHttpResponseHandler(){

                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
                    System.out.println(response.toString());
                    list.remove(position);
                    notifyDataSetChanged();
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    System.out.println(responseString);
                }
            });
        }
        else{
            RequestParams params = new RequestParams();
            params.put("action", "follow");
            InstagramRestClient.post(url, params, new JsonHttpResponseHandler(){

                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
                    System.out.println(response.toString());
                    list.remove(position);
                    notifyDataSetChanged();
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    System.out.println(responseString);
                }
            });
        }
    }
}

但是notifyDataSetChanged()没有刷新列表,并且没有删除该项目。

更新

整个适配器类

public class NonFollowersAdapter extends BaseAdapter {

private List<UserInfo> list;
private Context context;
private ImageLoader loader;
private boolean isUnfollow;
private Session session;

public NonFollowersAdapter(List<UserInfo> list, Context context, boolean isUnfollow){
    this.list = list;
    this.context = context;
    session = new Session(context);
    this.isUnfollow = isUnfollow;
    loader = AppController.getInstance().getImageLoader();
}
@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final NonFollowersHolder holder;
    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.listitem_nonfollowers, parent, false);

        holder = new NonFollowersHolder();
        holder.tv_fullname = (TextView)convertView.findViewById(R.id.tv_fullname);
        holder.tv_username = (TextView)convertView.findViewById(R.id.tv_username);
        holder.tv_bio = (TextView)convertView.findViewById(R.id.tv_bio);
        holder.userpic = (NetworkImageView)convertView.findViewById(R.id.iv_userpic);
        holder.action = (ImageView)convertView.findViewById(R.id.action);

        convertView.setTag(holder);
    }
    else{
        holder = (NonFollowersHolder)convertView.getTag();
    }
    holder.tv_fullname.setText(list.get(position).fullname);
    holder.tv_username.setText(list.get(position).username);
    String temp = list.get(position).bio;
    holder.tv_bio.setText(temp);
    holder.userpic.setImageUrl(list.get(position).profilepic, loader);

    if(isUnfollow == true) holder.action.setImageDrawable(context.getResources().getDrawable(R.drawable.bt_unfollow));
    else holder.action.setImageDrawable(context.getResources().getDrawable(R.drawable.bt_follow));

    holder.action.setOnClickListener(new MyListener(position));
    return convertView;
}


static class NonFollowersHolder{
TextView tv_fullname, tv_username, tv_bio;
NetworkImageView userpic;
    ImageView action;

}

private class MyListener implements View.OnClickListener {

    private int position;
    public MyListener(int position) {
        this.position = position;
    }

    @Override
    public void onClick(View v) {
        String url = AppProperties.APIURL+"/users/"+list.get(position).id
                +"/relationship?access_token="+session.getValue(AppProperties.ACCESS_TOKEN);
        if(isUnfollow){

            RequestParams params = new RequestParams();
            params.put("action", "unfollow");
            InstagramRestClient.post(url, params, new JsonHttpResponseHandler(){

                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
                    System.out.println(response.toString());
                    list.remove(position);
                    notifyDataSetChanged();
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    System.out.println(responseString);
                }
            });
        }
        else{
            RequestParams params = new RequestParams();
            params.put("action", "follow");
            InstagramRestClient.post(url, params, new JsonHttpResponseHandler(){

                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
                    System.out.println(response.toString());
                    list.remove(position);
                    notifyDataSetChanged();
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    System.out.println(responseString);
                }
            });
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

这个错误非常愚蠢。 notifyDataSetChanged()方法没有任何问题。我覆盖了onSuccess(int statusCode, Header[] headers, JSONArray response)方法,但我从服务器获得的响应是​​JSONObject。 因此该方法从未被执行,因此该项目未被删除。

将方法更改为onSuccess(int statusCode, Header[] headers, JSONObject response)时,一切正常。