【问题标题】:Populating ListView from Firebase Database从 Firebase 数据库填充 ListView
【发布时间】:2016-12-30 10:39:05
【问题描述】:

我有一个从 Firebase 数据库填充的 ListView:

这是我的代码:

final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl(".../comunidades");

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

        }
    };

这是这个数据库的 Firebase 控制台,分支:“comunidades”:

但是我在另一个片段中使用相同的代码来使用来自同一数据库的另一个分支的对象填充列表视图,但我收到了错误。

这是新代码:

final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl(".../Enclaves");

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


        }
    };

我应该改变什么来从分支“Enclaves”中获取键“Nombre_enclave”的值:

【问题讨论】:

  • 错误是什么? @NewAtFirebase
  • 您的 FirebaseListAdapter 不能保存字符串。它必须是映射到存储在 Firebase 中的模型的对象。字符串字段为Nombre_enclave 的对象
  • @NewAtFirebase:我在下面提供了答案。但对于未来的问题,请提供您的 JSON 作为文本而不是屏幕截图。您可以通过单击 Firebase 数据库控制台中的导出 JSON 链接轻松获取文本。将 JSON 作为文本使其可搜索,并允许我们在 cmets 和答案中轻松使用它。
  • @FrankvanPuffelen,感谢您的建议和回答。

标签: android firebase firebase-realtime-database firebaseui


【解决方案1】:

enclaves 下的内容不是 String+String 对的列表。相反,它是一个字符串+对象对的列表,每个对象都有一个 Comunidad_enclave、Descriptcion_enclave 等。

完成这项工作的最快方法是确定要显示的属性,然后覆盖parseSnapshot():

FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
        this.getActivity(),
        String.class,
        android.R.layout.simple_list_item_1,
        databaseReference
) {
    @Override
    protected String parseSnapshot(DataSnapshot snapshot) {
        return snapshot.child("Comunidad_enclave").getValue(String.class);
    }

    @Override
    protected void populateView(View v, String model, int position) {
        TextView textView = (TextView) v.findViewById(android.R.id.text1);
        textView.setText(model);
    }
};

解决此问题的更好方法是创建一个代表每个社区的类。最简单的形式可能如下所示:

class Comunidad {
    public String Comunidad_enclave;
    public String Descripcion__enclave;
    // TODO: the same for the other properties
}

通过这个类,你可以让你的适配器类型为Comunidad:

FirebaseListAdapter<Comunidad> firebaseListAdapter = new FirebaseListAdapter<Comunidad>(
        this.getActivity(),
        String.class,
        android.R.layout.simple_list_item_2,
        databaseReference
) {
    @Override
    protected void populateView(View v, Comunidad model, int position) {
        TextView textView1 = (TextView) v.findViewById(android.R.id.text1);
        textView1.setText(model.Comunidad_enclave);
        TextView textView2 = (TextView) v.findViewById(android.R.id.text2);
        textView2.setText(model.Descripcion_enclave);
    }
};

【讨论】:

  • 它工作,谢谢,但我不明白它是如何工作的。 parseSnapShot 方法是如何完成这项工作的?
  • 当您不覆盖parseSnapshot() 时,适配器会自动将DataSnapshot 转换为您在构造函数中指定的类(在您的情况下为String)。由于您的项目在第二种情况下不是简单的字符串,因此该转换不会产生任何结果。通过覆盖parseSnapshot(),您可以控制将哪些数据传递到populateView()。查看适配器的代码,它非常小。相关部分为:github.com/firebase/FirebaseUI-Android/blob/master/database/src/…
  • 感谢您的时间和精力。我在 SO 看到了你的贡献,我从中学到了很多。
猜你喜欢
  • 1970-01-01
  • 2017-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-13
  • 1970-01-01
相关资源
最近更新 更多