如何在Android中使用FirebaseListAdapter在ListView中显示Firebase数据库中项目的确切数量? [英] How to display the exact number of items from a Firebase Database in a ListView using FirebaseListAdapter with Android?

查看:129
本文介绍了如何在Android中使用FirebaseListAdapter在ListView中显示Firebase数据库中项目的确切数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的Firebase数据库,看起来像这样:

这是我的模特

 公共类ShoppingListModel {私有字符串shoppingListName;私有Map< String,String>usersMap;公共ShoppingListModel(){}public void setShoppingListName(String shoppingListName){this.shoppingListName = shoppingListName;}public String getShoppingListName(){返回shoppingListName;}公共无效setUsersMap(Map< String,String> usersMap){this.usersMap = usersMap;}公共Map< String,String>getUsersMap(){返回usersMap;}} 

我要做的就是在 ListView 中仅显示属于单个用户且值 true 的列表.使用以下代码:

 查询查询= databaseReference.child("ShoppingLists").orderByChild("shoppingListName");firebaseListAdapter = new FirebaseListAdapter< ShoppingListModel>(this,ShoppingListModel.class,android.R.layout.simple_list_item_1,查询){@Override受保护的void populateView(视图v,ShoppingListModel slm,int位置){对于(Map.Entry< String,String>条目):slm.getUsersMap().entrySet()){字符串键= entry.getKey();字符串值= entry.getValue();如果(key.equals("gmail @ gmail,com")&& value.equals("true")){((TextView)v.findViewById(android.R.id.text1)).setText(slm.getShoppingListName());}}}};ListView listView =(ListView)findViewById(R.id.list_view);listView.setAdapter(firebaseListAdapter); 

我得到一个空的视图,如下图所示.

如何正确显示 ListView 中的项目?

解决方案

您没有在问题"中指定它,但是我假设您的应用是一个购物清单,其中一个列表可以被许多用户使用./p>

我建议您重组数据库以简化查询:在存储每个用户的列表名称和ID的位置再添加一个节点.像这样:

 "userLists":{"gmail @ gmail":{"-Kbvasnd23sdj":"bbb","-Kbvasndaisdj":"aaa","-Kbvasxzvisdj":"ddd"},"ymail @ ymail":{"-Kbvasndaisxz":"ccc"}} 

这样,当您想从特定用户那里获取所有列表时,可以使用:

 查询查询= FirebaseDatabase.getInstance().getReference().child("userLists/gmail @ gmail"); 

您显然希望使用户能够单击某项并查看列表.您可以通过确保 userLists 中每个节点中的推入ID与 ShoppingLists 节点中的ID相同来实现.

简而言之:显示列表时,您是从userLists节点读取数据的.显示列表详细信息时,您将从ShoppingLists节点读取数据.在Firebase上创建数据库时必须记住的几点是:

查看后构造数据.

有关如何操作他的更多详细信息,建议您观看 SQL Developers系列的Firebase数据库.

I have a very simple Firebase Database which looks like this:

This is my model:

public class ShoppingListModel {
    private String shoppingListName;
    private Map<String, String> usersMap;

    public ShoppingListModel() {}

    public void setShoppingListName(String shoppingListName) {this.shoppingListName = shoppingListName;}
    public String getShoppingListName() {return shoppingListName;}

    public void setUsersMap(Map<String, String> usersMap) {this.usersMap = usersMap;}
    public Map<String, String> getUsersMap() {return usersMap;}
}

All i want to do, is to display in a ListView only the lists that belong to a single user and have the value of true. With this code:

Query query = databaseReference.child("ShoppingLists").orderByChild("shoppingListName");
firebaseListAdapter = new FirebaseListAdapter<ShoppingListModel>(this, ShoppingListModel.class, android.R.layout.simple_list_item_1, query) {
        @Override
        protected void populateView(View v, ShoppingListModel slm, int position) {
            for (Map.Entry<String, String> entry : slm.getUsersMap().entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                if (key.equals("gmail@gmail,com") && value.equals("true")) {
                    ((TextView)v.findViewById(android.R.id.text1)).setText(slm.getShoppingListName());
                }
            }
        }
    };
    ListView listView = (ListView) findViewById(R.id.list_view);
    listView.setAdapter(firebaseListAdapter);

I get an empty view as shown in the image below.

How can i display the items in the ListView correctly?

解决方案

You didn't specify it on the Question, but I'm assuming your app is a Shopping List where a single list can be used by many users.

I suggest you restructure your database to simplify queries: Add one more node where you store each user's list name and id. Like this:

"userLists" : {
    "gmail@gmail" : {
      "-Kbvasnd23sdj" : "bbb",
      "-Kbvasndaisdj" : "aaa",
      "-Kbvasxzvisdj" : "ddd"
    },
    "ymail@ymail" : {
      "-Kbvasndaisxz" : "ccc"
    }
  }

This way, when you want to get all the lists from a specific user, you'd use:

Query query = FirebaseDatabase.getInstance().getReference().child("userLists/gmail@gmail");

And you'd obviously want to give the user the ability to click on an item and see the list. You can do that by making sure that the push id in each node from userLists is the same as the id from the ShoppingLists node.

In short: When you're displaying the list, you read the data from the userLists node. When you're displaying the list details, you read data from the ShoppingLists node. Something that you must keep in mind when creating databases on Firebase is:

Structure your data after your view.

For more details on how to do his, I recommend you watch the Firebase Database for SQL Developers series.

这篇关于如何在Android中使用FirebaseListAdapter在ListView中显示Firebase数据库中项目的确切数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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