【问题标题】:How can I load an AutoCompleteTextView from a list of Firebase data?如何从 Firebase 数据列表中加载 AutoCompleteTextView?
【发布时间】:2016-12-07 17:55:58
【问题描述】:

如果我想从 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 适配器可以解决这个问题吗?

【问题讨论】:

  • 您必须自己编写代码。使用 startAtendAt 进行 Firebase 查询是可能的方式。见firebase.google.com/docs/database/android/…
  • @FrankvanPuffelen 所以 startAt() 从指定的键中获取所有值?
  • @FrankvanPuffelen 你能给我一个例子吗?
  • @FrankvanPuffelen 我做到了。我想知道您对我的回答中提到的代码有何看法。

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


【解决方案1】:
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) {

    }
});

【讨论】:

    【解决方案2】:

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

    这是我的数据库: 按照下面代码中的cmets来实现我所需要的:

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

    【讨论】:

    • 感谢您的高效解决方案和您的时间。你为我节省了 6 个小时!如果您还在 Firebase 中更新 AutoCompleteOptions,则需要在 public void onDataChange(DataSnapshot dataSnapshot) {... 之后直接添加 autoComplete.clear();。否则,当再次触发侦听器时,结果会简单地添加到已填充的 ArrayAdapter 中,将先前添加到 ArrayAdapter 的所有条目加倍。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 2020-05-04
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多