使用RealmObject的Android列表过滤器IllegalStateException

时间:2017-03-27 09:13:16

标签: java android filter realm baseadapter

我正在尝试过滤填充了某些自定义对象的ListView,这些对象正在扩展RealmObject

我的filter仅适用于传递的空String,每当我将一些文字传递给过滤器时,我都会遇到以下异常:

  

方法抛出'java.lang.IllegalStateException'异常。无法评估io.realm.WarehouseRealmProxy.toString()

我的自定义项称为Warehouse

public class Warehouse extends RealmObject {
    public Warehouse(){}

    @PrimaryKey
    private String ID;
    private String NAME;
    private String CODE;
    //{...more useless stuffs, getters, setters...}
}

这是我的adapterFilter类:

public class ListWarehouseAdapter extends BaseAdapter implements Filterable {

    private List<Warehouse> originalData;
    private List<Warehouse> filteredData;
    private Context ctx;
    private WarehouseFilter warehouseFilter = new WarehouseFilter();
    private final Object mLock = new Object();


    public ListWarehouseAdapter(Context context, int textViewResourceId) {
//        super(context, textViewResourceId);
        originalData = null;
        filteredData = null;
        ctx = context;
    }

    public ListWarehouseAdapter(Context context, int resource, List<Warehouse> items) {
//        super(context, resource, items);
        originalData = new ArrayList<>(items);
        filteredData = new ArrayList<>(items);
        ctx = context;
    }

    static class ViewHolder{
        TextView tvWarehouseCode;
        TextView tvWarehouseName;
    }

    @NonNull
    @Override
    public Filter getFilter() {
        return warehouseFilter;
    }

    @Override
    public int getCount() {
        synchronized (mLock) {
            if (filteredData != null) {
                return filteredData.size();
            } else {
                return 0;
            }
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

//        View v = convertView;
        ViewHolder viewHolder;

        if(convertView == null || convertView.getTag() == null){
            LayoutInflater layoutInflater = LayoutInflater.from(ctx);
            convertView = layoutInflater.inflate(R.layout.list_warehouse_item, null);

            viewHolder = new ViewHolder();
            viewHolder.tvWarehouseCode = (TextView) convertView.findViewById(tvWarehouseCode);
            viewHolder.tvWarehouseName = (TextView) convertView.findViewById(tvWarehouseName);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Warehouse w = getItem(position);

        if (w != null) {
            if (viewHolder.tvWarehouseCode != null) {
                viewHolder.tvWarehouseCode.setText(w.getCODE());
            }

            if (viewHolder.tvWarehouseName != null) {
                viewHolder.tvWarehouseName.setText(w.getNAME());
            }
        }
        return convertView;
    }

    @Nullable
    @Override
    public Warehouse getItem(int position) {
        synchronized (mLock) {
            if (filteredData == null) {
                return null;
            }
            try {
                return filteredData.get(position);
            } catch (Exception ignored) {
                return null;
            }
        }
    }

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

    private class WarehouseFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {



            String filterString = constraint.toString().toLowerCase();
            FilterResults results = new FilterResults();

            if(constraint.length() == 0){
                synchronized (mLock) {
                    results.values = originalData;
                    results.count = originalData.size();
                }
            }else {
                synchronized (mLock) {
                    final List<Warehouse> list = new ArrayList<>(originalData);

                    int count = list.size();
                    final ArrayList<Warehouse> nlist = new ArrayList<Warehouse>(count);

                    for (Warehouse w : list) {
                        if (w.getNAME().toLowerCase().contains(filterString) || w.getCODE().toLowerCase().contains(filterString)) {
                            nlist.add(w);
                        }
                    }

                    results.values = nlist;
                    results.count = nlist.size();
                }
            }
            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            synchronized (mLock) {
                if (results.values != null) {
                    filteredData = (ArrayList<Warehouse>) results.values;
                } else {
                    filteredData = new ArrayList<>();
                }
                notifyDataSetChanged();
            }
        }

    }
}

Filter的{​​{1}}方法中,我在尝试检查PerformFiltering对象时遇到了上述异常,该对象是list列表的副本。此外,当尝试访问originalData中的项目时,我收到了错误。

我做错了什么?

由于

0 个答案:

没有答案