realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

Realm AutoComplete TextView Adapter

Open koz01 opened this issue 10 years ago • 2 comments

I'm trying to populate an AutocompleteTextView with a list of "String". I'm extending the Adapter as follows:

  public class RealmAutoCompleteSearchAdapter extends ArrayAdapter<String> {
  private final String MY_DEBUG_TAG = "StringAdapter";
  private ArrayList<String> items;
  private ArrayList<String> itemsAll;
  private ArrayList<String> suggestions;
  private int viewResourceId;
public RealmAutoCompleteSearchAdapter(Context context, int viewResourceId, ArrayList<String> items) {
    super(context, viewResourceId, items);
    this.items = items;
    this.itemsAll = (ArrayList<String>) items.clone();
    this.suggestions = new ArrayList<String>();
    this.viewResourceId = viewResourceId;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(viewResourceId, null);
    }
    String name = items.get(position);
    if (name != null) {
        TextView stringNameLabel = (TextView) v.findViewById(R.id.textview);
        if (stringNameLabel != null) {
            stringNameLabel.setText(name);
        }

    }
    return v;
}

@Override
public Filter getFilter() {
    return nameFilter;
}

Filter nameFilter = new Filter() {
    @Override
    public String convertResultToString(Object resultValue) {
        String str = ((String)(resultValue));
        return str;
    }
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        if(constraint != null) {
            suggestions.clear();
            for (String name : itemsAll) {
                if(name.trim().toLowerCase().startsWith(constraint.toString().toLowerCase())){
                    suggestions.add(name);
                }
            }
            FilterResults filterResults = new FilterResults();
            filterResults.values = suggestions;
            filterResults.count = suggestions.size();
            return filterResults;
        } else {
            return new FilterResults();
        }
    }
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        ArrayList<String> filteredList = (ArrayList<String>) results.values;
        if(results != null && results.count > 0) {
            clear();
            for (String c : filteredList) {
                add(c);
            }
            notifyDataSetChanged();
        }
    }
};

} ---**************************************************************************************** List data from realmresult method is the following..

public ArrayList<String> getStringListData(Realm realm) {

    RealmResults<Product> result = realm.where(Product.class).findAll();
    result.sort("name", Sort.ASCENDING);
    ArrayList<String> findStringList = new ArrayList<String>();
    for (Product product : result) {
        findStringList.add(product.getProudct_name().replace("\"",""));
    }
    return findStringList;
}

The problem is that data filter and text change performance is very slow. I have 800 records. What I am missing here? Please help me and how can i do more performance.

Thank you. Michael.

koz01 avatar May 26 '16 12:05 koz01

It's slow because you're loading the entire Realm into memory. You're also neglecting the return value of sort("name") which means your results aren't actually sorted, as sort() returns a new realm results.

Anyways, this is partly a duplicate of https://github.com/realm/realm-java/issues/2887 , where I think I've answered the question.

Zhuinden avatar May 26 '16 19:05 Zhuinden

Dup to #2887

beeender avatar May 27 '16 01:05 beeender