Zar Saeed
Zar Saeed

Reputation: 110

AutoCompleteTextView throwing an error with FirebaseListAdapter

I am trying to populate the drop down of AutoCompleteTextView with FirebaseListAdapter it's successfully populated with data from firebase but when i clicked on item in drop down list the error come up with following stack trace:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.Filter.convertResultToString(java.lang.Object)' on a null object reference

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

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

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

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

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

at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5942) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at 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;
}

}

Calling of FirebaseListAdapter from MainActivity

    @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("")){

            }

        }

Upvotes: 1

Views: 301

Answers (1)

Zar Saeed
Zar Saeed

Reputation: 110

it was a very tinny error after all day research i found the solution ,I made amendments here in FirebaseListAdapter to remove the error.

**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) {

    }
};

}

Upvotes: 0

Related Questions