Search code examples
androidperformancefirebasespinnersearchable

Creating a Searchable spinner that Loads from Fire Base


I am trying to create a searchable spinner that loads the counties information from firebase. I am new at android development and I based what I did on various videos on youtube and a stack overflow post. Heres what I have so far in my activity:

public class Preferences extends AppCompatActivity {

    SearchableSpinner spcounties;
    private DatabaseReference mDatabase;
    List<String> countyList = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preferences);

        spcounties = (SearchableSpinner) findViewById(R.id.spcounties);
        mDatabase = FirebaseDatabase.getInstance().getReference();
        mDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    String county = ds.child("County").getValue(String.class);
                    countyList.add(county);
                }
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, countyList);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spcounties.setAdapter(adapter);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(Preferences.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
            }

        });
    }
}

My DB is structured like so:
enter image description here

Right now my app crashes when the searchable spinner is clicked on.

Stack trace:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.jws.documentmarketplace, PID: 17515
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:454)
        at android.widget.ArrayAdapter.getView(ArrayAdapter.java:416)
        at android.widget.AbsListView.obtainView(AbsListView.java:2405)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1417)
        at android.widget.ListView.onMeasure(ListView.java:1323)
        at android.view.View.measure(View.java:25185)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6821)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
        at android.view.View.measure(View.java:25185)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6821)
        at android.widget.Fra

Solution

  • I changed this line and it worked.

    mDatabase = FirebaseDatabase.getInstance().getReference("Counties/");