如何从Firebase数据列表中加载AutoCompleteTextView?

时间:2016-08-02 11:38:08

标签: android firebase firebase-realtime-database autocompletetextview firebaseui

如果我想从Firebase将数据列表加载到android中的AutoCompleteTextView,我该怎么做?

我的想象力如何:

我使用类似于FirebaseRecyclerAdapter的内容获取数据,并将该适配器设置为ACTV。例如,如果我有这些数据:

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."
   }
}

当我输入“Hakuna Matata”时,ACTV应该有两个陈述作为建议。这有什么特殊的Firebase适配器吗?

2 个答案:

答案 0 :(得分:9)

经过6个小时的研究,我终于感谢this link

这是我的数据库: enter image description here 按照以下代码中的注释来实现我的需求:

//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);

答案 1 :(得分: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) {

    }
});