AutoCompleteTextView使用FirebaseListAdapter

时间:2017-04-28 13:37:47

标签: android firebase autocompletetextview

我正在尝试使用FirebaseListAdapter填充AutoCompleteTextView的下拉列表,它已成功填充来自firebase的数据但是当我点击下拉列表中的项目时,错误会出现以下堆栈跟踪:

  

java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.CharSequence android.widget.Filter.convertResultToString(java.lang.Object)'

     

在android.widget.AutoCompleteTextView.convertSelectionToString(AutoCompleteTextView.java:825)

     

在android.widget.AutoCompleteTextView.buildImeCompletions(AutoCompleteTextView.java:1140)   在android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1096)

     

在android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:984)

     

在android.widget.AutoCompleteTextView.access $ 900(AutoCompleteTextView.java:91)

     

在android.widget.AutoCompleteTextView $ PopupDataSetObserver $ 1.run(AutoCompleteTextView.java:1306)

     

在android.os.Handler.handleCallback(Handler.java:739)   在android.os.Handler.dispatchMessage(Handler.java:95)   在android.os.Looper.loop(Looper.java:145)   在android.app.ActivityThread.main(ActivityThread.java:5942)                                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                                   在java.lang.reflect.Method.invoke(Method.java:372)    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1399)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

FireBaseListAdapter

public class SearchListAdapter extends FirebaseListAdapter<Product> implements Filterable {
private static final String TAG = "SearchListAdapter";
public SearchListAdapter(Activity activity, Class<Product> modelClass, int modelLayout, Query ref){
    super(activity,modelClass,modelLayout,ref);
    this.mActivity=activity;
}
@Override
protected void populateView(View v, Product model, int position) {
    TextView textView= (TextView) v.findViewById(R.id.txt_item);
    Log.e(TAG,model.getName());
    textView.setText(model.getName());
}


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

}

从MainActivity调用FirebaseListAdapter

    @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String searchString=autoCompleteTextView.getText().toString();
            if (!searchString.equals("")) {
                ref = FirebaseDatabase.getInstance().getReference().child("products").orderByChild("name").startAt(searchString).endAt(searchString + "\uf8ff");
                SearchListAdapter searchListAdapter=new SearchListAdapter(MainActivity.this,Product.class,R.layout.single_list_item,ref);
                autoCompleteTextView.setAdapter(searchListAdapter);
                autoCompleteTextView.showDropDown();
                autoCompleteTextView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        Log.e(TAG,"itemClicked");
                        autoCompleteTextView.setText("");
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
            }
            else if (searchString.equals("")){

            }

        }

1 个答案:

答案 0 :(得分:0)

在我找到解决方案的整天研究后,这是一个非常小的错误,我在FirebaseListAdapter中进行了修改,以消除错误。

**FirebaseListAdapter**
public class SearchListAdapter extends FirebaseListAdapter<Product> implements Filterable {
private static final String TAG = "SearchListAdapter";
public SearchListAdapter(Activity activity, Class<Product> modelClass, int modelLayout, Query ref){
    super(activity,modelClass,modelLayout,ref);
    this.mActivity=activity;
}
@Override
protected void populateView(View v, Product model, int position) {
    TextView textView= (TextView) v.findViewById(R.id.txt_item);
    Log.e(TAG,model.getName());
    textView.setText(model.getName());
}

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

Filter filter=new Filter() {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        return null;
    }

    @Override
    public CharSequence convertResultToString(Object resultValue) {
        String str = ((Product)(resultValue)).getName();
        return str;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {

    }
};

}