2016-04-29 12 views
0

フィルタリングにを使用しようとしていますが、フィルタに問題があります。フィルタリングされたものの代わりにArrayListのすべてのアイテムを返します。以下は私のコードです:AutoCompleteTextViewテキストフィルタリング

Filter nameFilter = new Filter() { 
    @Override 
    public String convertResultToString(Object resultValue) { 
     String str = ((State)(resultValue)).getName(); 
     Log.e("Conv"+TAG, str); 
     return str; 
    } 
    @Override 
    protected FilterResults performFiltering(CharSequence constraint) { 
     if(constraint != null) { 
      suggestions.clear(); 
      for (State customer : itemsAll) { 
       Log.e("Ite "+TAG, "" + itemsAll.size()); 
       Log.e("Cons " + TAG, constraint.toString()); 
       Log.e("Sta" + TAG, customer.getName()); //This is always null 
       if(customer.getName().toLowerCase().contains(constraint.toString().toLowerCase())) { 
        suggestions.add(customer); 
       } 
      } 
      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) { 
     List<State> filteredList = (ArrayList<State>) results.values; 
     if(results != null && results.count > 0) { 
      clear(); 
      Log.e("D "+TAG, ""+results.count); 
      for (State c : filteredList) { 
       Log.e("The "+TAG, c.getName() + " " + c.getId()); 
       add(c); 
       notifyDataSetChanged(); 
      } 
     } else { 
      Log.e(TAG, "Empty Filter"); 
      notifyDataSetChanged(); 
     } 
    } 
}; 

そして私は周りのロギングを開始し、私はこの行Log.e("Cons " + TAG, constraint.toString());と、この行Log.e("Ite "+TAG, "" + itemsAll.size());は決してnullである一方、この行は常にLog.e("Sta" + TAG, customer.getName()); //This is always null nullです気づきました。テキストが渡されない限り、Constraintをnullにすることはできません。しかし、itemsAll.size()がnullでも空でもない場合、最初のオブジェクトはnullになります。私は混乱しています。以下、私のList初期

private static final String TAG = "StateAdapter"; 
private ArrayList<State> items; 
private ArrayList<State> itemsAll; 
private ArrayList<State> suggestions; 
private Context context; 
private int viewResourceId; 

public StateAutoCompleteAdapter(Context context, int resource, int textViewResourceId, ArrayList<State> objects) { 
    super(context, resource, textViewResourceId, objects); 
    this.context = context; 
    this.items = objects; 
    this.itemsAll = new ArrayList<State>(items); 
    this.suggestions = new ArrayList<State>(); 
    this.viewResourceId = resource; 
} 

getView機能について

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); 
    } 
    State customer = items.get(position); 
    if (customer != null) { 
     TextView customerNameLabel = (TextView) v.findViewById(R.id.bakerName); 
     TextView bakerAddress = (TextView) v.findViewById(R.id.bakerAddress); 
     if (customerNameLabel != null) { 
//    Log.i(MY_DEBUG_TAG, "getView Customer Name:"+customer.getName()); 
      customerNameLabel.setText(customer.getName()); 
      bakerAddress.setVisibility(View.GONE); 
     } 
    } 
    return v; 
} 
+0

を行きます。あなたはpublishResults()で何が得られるかは言及していませんでしたが、私はあなたが "空のフィルター"を取得していると思いますか?また、私はArrayAdapterを拡張しようとしています。カスタムアダプターは標準的なシナリオ用に設計されているため、カスタムフィルターを使用する予定はありません。 – Luksprog

+0

あなたのデータはどこから取得しましたか? – pskink

+0

@pskink私は 'Realmdb'からデータを取得しています。だから私はカウントを得ることができるのです –

答えて

0

()メソッドは、問題を引き起こしている可能性があります明らかです。ですから、どのアレイリストをクリーニングしているかのコードを確認してください。

あなたはgetView()State customer = items.get(position);を使用してsuggestions.add(customer);を使用しているので、別の問題がperformFiltering(CharSequence constraint)

であるが、この情報がお役に立てば幸いです。

0

だから、私はこれをやって自分自身で問題を解決しました。 customer.getName()は、あなたがそれ以下NullPointerExceptionが1つのラインを持っているでしょうnullの場合はそう、私は通常のBaseAdapterを使用し、そうFilterableを拡張し、そこAutoCompleteTextViewRealmDBを使用する方法が必要な場合がありもののためにあなたは

public class LgasAutoCompleteAdapter extends BaseAdapter implements Filterable { 

private static final String TAG = "LgasAutoComp"; 
private Context mContext; 
private List<Lga> mResult = new ArrayList<>(); 
private LayoutInflater inflater; 

public LgasAutoCompleteAdapter(Context mContext) { 
    this.mContext = mContext; 
} 

@Override 
public int getCount() { 
    return mResult.size(); 
} 

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

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

@Override 
public View getView(int position, View view, ViewGroup parent) { 
    if (inflater == null) 
     inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    if (view == null) 
     view = inflater.inflate(R.layout.item_baker_autocomplete, parent, false); 

    Lga lga = mResult.get(position); 

    TextView customerNameLabel = (TextView) view.findViewById(R.id.bakerName); 
    TextView bakerAddress = (TextView) view.findViewById(R.id.bakerAddress); 

    if (lga != null) { 
     if (customerNameLabel != null) { 
//    Log.i(MY_DEBUG_TAG, "getView Customer Name:"+customer.getName()); 
      customerNameLabel.setText(lga.getName()); 
      bakerAddress.setVisibility(View.GONE); 
     } 
    } 

    return view; 
} 

@Override 
public Filter getFilter() { 
    return new Filter() { 
     @Override 
     protected FilterResults performFiltering(CharSequence charSequence) { 
      return null; 
     } 

     @Override 
     protected void publishResults(CharSequence constraint, FilterResults filterResults) { 
      if (constraint != null) { 
       //String query = constraint.toString().toLowerCase(); 
       mResult = filterStates(constraint.toString()); 
       Log.e(TAG, ""+mResult.size()); 
       notifyDataSetChanged(); 
      } else { 
       notifyDataSetInvalidated(); 
      } 
     } 
    }; 
} 

@NonNull 
private List<Lga> filterStates(String query) { 
    Realm mRealm = RealmUtils.getRealmInstance(mContext); 
    return mRealm.where(Lga.class) 
      .contains("name", query) 
      .or() 
      .beginsWith("name", query) 
      .findAll(); 
} 
} 
関連する問題