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

3
如果我想从 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适配器可用于此?

你将需要亲自编写这段代码。Firebase 查询中的 startAtendAt 是可能的解决方案。请参阅 https://firebase.google.com/docs/database/android/retrieve-data#filtering_data。 - Frank van Puffelen
@FrankvanPuffelen,所以startAt()会获取指定键的所有值吗? - Ali Bdeir
@FrankvanPuffelen,你能给我提供一个示例的链接吗? - Ali Bdeir
@FrankvanPuffelen,我做到了。我想知道您对我在答案中提到的代码有什么看法。 - Ali Bdeir
2个回答

15

经过6个小时的研究,我终于做到了,这要感谢这个链接

这是我的数据库: 在这里输入图片描述 按照以下代码中的注释来实现我所需的内容:

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

2
感谢您提供高效的解决方案和宝贵的时间。您为我节省了6个小时!如果您还在Firebase中更新“AutoCompleteOptions”,那么您需要在“public void onDataChange(DataSnapshot dataSnapshot) {...”之后立即添加“autoComplete.clear();”。否则,当监听器再次触发时,结果将仅添加到已填充的ArrayAdapter中,导致所有先前添加到ArrayAdapter中的条目翻倍。 - Christopher Mills

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

    }
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接