Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从Firebase数据列表加载AutoCompleteTextView?_Android_Firebase_Firebase Realtime Database_Autocompletetextview_Firebaseui - Fatal编程技术网

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

Android 如何从Firebase数据列表加载AutoCompleteTextView?,android,firebase,firebase-realtime-database,autocompletetextview,firebaseui,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!" }

如果我想从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适配器吗?

经过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);
//没什么特别的,创建数据库引用。
DatabaseReference database=FirebaseDatabase.getInstance().getReference();
//根据您的上下文和Android提供的下拉菜单的简单布局创建一个新的ArrayAdapter
final ArrayAdapter autoComplete=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1);
//在找到所有push()键之前对根进行子对象化,并添加ValueEventListener()
database.child(“自动完成选项”).addValueEventListener(新的ValueEventListener(){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
//基本上,这表示“对于DataSnapshot中的每个DataSnapshot*数据*,执行方法内部的操作。
对于(DataSnapshot suggestionSnapshot:DataSnapshot.getChildren()){
//通过对要获取的字符串的键进行子化来获取建议。
String suggestion=suggestionSnapshot.child(“suggestion”).getValue(String.class);
//将检索到的字符串添加到列表中
自动完成。添加(建议);
}
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
}
});
AutoCompleteTextView ACTV=(AutoCompleteTextView)findViewById(R.id.ACTV);
ACTV.setAdapter(自动完成);
reference=FirebaseDatabase.getInstance().getReference();
ArrayList=新建ArrayList();
ArrayAdapter adp;
uname=(AutoCompleteTextView)findviewbyd(R.id.uname);
adp=new ArrayAdapter(这是android.R.layout.select\u dialog\u item,list);
uname.setThreshold(1);
取消设置适配器(adp);
addValueEventListener(新的ValueEventListener(){
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot){
对于(DataSnapshot dsp:DataSnapshot.getChildren())
{
如果(dsp!=null){
对于(DataSnapshot dsp1:dsp.getChildren()){
list.add(String.valueOf(dsp1.getKey());
adp.notifyDataSetChanged();
}
}
}
}
@凌驾
已取消的公共void(@NonNull DatabaseError DatabaseError){
}
});

您必须自己编写代码。使用
startAt
endAt
进行Firebase查询是可能的方法。请参阅@FrankvanPuffelen so startAt()从指定的键获取所有值?@FrankvanPuffelen您能将我链接到一个示例吗?@FrankvanPuffelen我做到了。我想知道您对我在回答中提到的代码的看法。感谢高效的解决方案和您的时间。您为我节省了6个小时!如果您还在Firebase中更新
自动完成选项
,您将希望将
autoComplete.clear();
直接添加到
public void onDataChange(DataSnapshot DataSnapshot){…
。否则,当侦听器再次启动时,结果只需添加到已填充的ArrayAdapter,将之前添加到ArrayAdapter的所有条目加倍。
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) {

    }
});