Android自定义适配器过滤器很好,但listview没有更新

时间:2015-07-08 13:54:58

标签: android listview android-listview

我有一个自定义适配器,可以显示电话列表中的联系人和按钮。正如标题所说过滤器工作正常(列表计数更改)但listview保留相同的数据,如果count为3 listview显示前3个条目。

适配器类:

     public class UserCustomAdapter extends ArrayAdapter<User> {
     Context context;
     int layoutResourceId;
     ArrayList<User> data = new ArrayList<User>();

    public UserCustomAdapter(Context context, int layoutResourceId,
    ArrayList<User> data) {
      super(context, layoutResourceId, data);
      this.layoutResourceId = layoutResourceId;
      this.context = context;
      this.data = data;
    }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    UserHolder holder = null;

 if (row == null) {
    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
    row = inflater.inflate(layoutResourceId, parent, false);
    holder = new UserHolder();
    holder.textName = (TextView) row.findViewById(R.id.textView1);
    holder.number = (TextView) row.findViewById(R.id.textView2);
    holder.textLocation = (TextView) row.findViewById(R.id.textView3);
    holder.btnEdit = (Button) row.findViewById(R.id.Share);
    holder.btnInvite = (Button) row.findViewById(R.id.button2);
    row.setTag(holder);
 } else {
    holder = (UserHolder) row.getTag();
 }
  final User user = data.get(position);
  holder.textName.setText(user.getName());
  holder.number.setText(user.getNumber());
  holder.textLocation.setText(user.getLocation());

  if(user.isMember())
     holder.btnInvite.setVisibility(View.INVISIBLE);


  return row;
}

用户类是

public class User {
     String name;
     String number;
     String location;
     boolean member;
  @Override
    public final String toString(){
     Log.e("Sent to filter", name);
      return name;
     }
+plus all getters and setters
}

1 个答案:

答案 0 :(得分:1)

你应该使用

final User user = getItem(position);

而不是

final User user = data.get(position);

因为您依赖于Filter ArrayAdapter的实施。