3

If I want to load a list of data into an AutoCompleteTextView in android from Firebase, how will I do that?

How I'd imagine it:

I get the data using something similar to a FirebaseRecyclerAdapter, and set that adapter to the ACTV. For example, if I have this data:

AutoComplete:{
  JKDJKADJKADFJAKD:{
      name:"Hakuna Matata , your orangeness -- I mean your highness, Mr. Trump!"
   }
  JDKIKSLAIJDKDIKA:{
      name:"Hakuna Matata! I ask not to take offense by the previous statement."
   }
}

The ACTV should have both statements as suggestions when I type in "Hakuna Matata". Is there any special Firebase adapter for this?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117

2 Answers2

15

After 6 hours of research, I finally did it thanks to this link.

Here's my database: enter image description here Follow the comments in the following code to achieve what I needed:

//Nothing special, create database reference.
    DatabaseReference database = FirebaseDatabase.getInstance().getReference();
    //Create a new ArrayAdapter with your context and the simple layout for the dropdown menu provided by Android
    final ArrayAdapter<String> autoComplete = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1);
    //Child the root before all the push() keys are found and add a ValueEventListener()
    database.child("AutoCompleteOptions").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //Basically, this says "For each DataSnapshot *Data* in dataSnapshot, do what's inside the method.
            for (DataSnapshot suggestionSnapshot : dataSnapshot.getChildren()){
                //Get the suggestion by childing the key of the string you want to get.
                String suggestion = suggestionSnapshot.child("suggestion").getValue(String.class);
                //Add the retrieved string to the list
                autoComplete.add(suggestion);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    AutoCompleteTextView ACTV= (AutoCompleteTextView)findViewById(R.id.actv);
    ACTV.setAdapter(autoComplete);
Community
  • 1
  • 1
Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117
  • 2
    Thanks for the efficient solution and your time. You saved me 6 hours! If you are also updating the `AutoCompleteOptions` in Firebase, you will want to add `autoComplete.clear();` straight after `public void onDataChange(DataSnapshot dataSnapshot) {...`. Otherwise when the listener is fired again, the results are simply added to the already populated ArrayAdapter, doubling up on all entries that were previously added to the ArrayAdapter. – Christopher Mills Nov 19 '17 at 13:49
0
reference=FirebaseDatabase.getInstance().getReference();
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter <String> adp;
uname=(AutoCompleteTextView) findViewById(R.id.uname);
adp = new ArrayAdapter<>(this,android.R.layout.select_dialog_item,list);
uname.setThreshold(1);
uname.setAdapter(adp);
reference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot dsp : dataSnapshot.getChildren())
        {
            if(dsp!= null){
                for (DataSnapshot dsp1 : dsp.getChildren()){
                    list.add(String.valueOf(dsp1.getKey()));
                    adp.notifyDataSetChanged();
                }

        }
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});
KJ Patel
  • 1
  • 1