从Firebase数据库填充微调器 [英] Populatate the spinner from Firebase database

查看:50
本文介绍了从Firebase数据库填充微调器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class MainActivity extends AppCompatActivity {
DatabaseReference reference;
Spinner areaSpinner;
ArrayList<String> areas = new ArrayList<>();


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    reference = FirebaseDatabase.getInstance().getReferenceFromUrl("https://vbspinner.firebaseio.com/Location");

    areaSpinner = (Spinner) findViewById(R.id.spinner);
    DatabaseReference mref = reference.child("areas");

    FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(this, String.class, android.R.layout.simple_spinner_item, mref) {
        @Override
        protected void populateView(View v, String model, int position) {
            ((TextView)findViewById(android.R.id.text1)).setText(model);

        }
    };
    areaSpinner.setAdapter(firebaseListAdapter);
}

我的Firebase数据库:

My Firebase database:

我的构造函数类代码是

public class VBSpinner extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        if(!com.google.firebase.FirebaseApp.getApps(this).isEmpty()){
            FirebaseDatabase.getInstance().setPersistenceEnabled(true);
        }

我希望我的微调器值来自Firebase数据库( Kvp Tn Tvl ,依此类推).我需要在哪里更改数据?

I would like that my spinner values are from the Firebase database (Kvp, Tn,Tvl, so on). Where do I need to change the data?

推荐答案

reference = FirebaseDatabase.getInstance()
     .getReferenceFromUrl("https://vbspinner.firebaseio.com/Location");

在上面的代码中,您使 reference 对象指向"Location" (我认为您正在尝试这样做.但是在以下代码中:

In above code, you make reference object point to "Location" (which I think what you're trying to do. But in the following code:

DatabaseReference mref = reference.child("areas");

您再创建一个称为 mref 的引用,该引用(由于 child("areas"))指向"https://vbspinner.firebaseio.com/位置/区域" .因此,如果在 FirebaseListAdapter 中使用 mref ,则它将返回null,因为".../Location/areas" 中没有值.

You make another reference called mref that (because of child("areas")) point to "https://vbspinner.firebaseio.com/Location/areas". So if you use mref in your FirebaseListAdapter, it will return null because there is no value in ".../Location/areas".

您应该在 FirebaseListAdapter 中使用 reference 使其正常工作.

You should use reference in FirebaseListAdapter to make it work.

这篇关于从Firebase数据库填充微调器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆