如何从 Firebase 数据列表加载 AutoCompleteTextView?
How can I load an AutoCompleteTextView from a list of Firebase data?
如果我想从 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);
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) {
}
});
如果我想从 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);
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) {
}
});