将 FirebaseRecyclerViewAdapter 耦合到布尔/字符串 Map.Entry [英] Coupling FirebaseRecyclerViewAdapter to a Boolean/String Map.Entry

查看:19
本文介绍了将 FirebaseRecyclerViewAdapter 耦合到布尔/字符串 Map.Entry的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序的许多位置使用了 com.firebaseui:firebase-ui:0.2.0 库中的 FirebaseRecyclerViewAdapter.我的问题是如何在查询参数返回多个索引"条目值(Map.Entry)的情况下应用它.

如 Firebase 文档中所述 (

有关完整代码,请参阅此 repoActivity34559171>.

I'm using the FirebaseRecyclerViewAdapter from the com.firebaseui:firebase-ui:0.2.0 library in many locations of my app. My question is how to apply it in situations where the query argument returns a number of 'index' entry values (Map.Entry).

As indicated in the Firebase documentation (https://www.firebase.com/docs/android/guide/structuring-data.html), I'm using indexes to keep the data structure flat. This leads to a situation in which I need to bind these indexes to my ViewHolder. Within the populate view method, I'm using the key of the index to retrieve the data to fill the viewholder with.

I'm having trouble to create the adapter, not sure how to define 'modelClass' argument in case of a Boolean/String Map.Entry:

mAdapter = new FirebaseRecyclerViewAdapter<Map.Entry<String, Boolean>, ItemViewHolder>(???.class, R.layout.my_card, ItemViewHolder.class, getItemIndexQuery(mKey) ) {
...
}

解决方案

Your value is a Boolean, not a Map. So your comment is correct: you will need to specify Boolean/Boolean.class for the value type. To be able to then look up the key, you will need to upgrade to FirebaseUI-Android 0.2.2. In that release we added a populateViewHolder(VH, T, int) overload that gets the position of the item as a parameter. With that, you can look up the key of the item.

Say this is your JSON structure:

{
  "items": {
    "pushid1": "Fri Jan 01 2016 16:40:54 GMT-0800 (PST)",
    "pushid2": "Fri Jan 01 2016 16:41:07 GMT-0800 (PST)",
    "pushid3": "Fri Jan 01 2016 16:41:25 GMT-0800 (PST)",
    "pushid4": "Fri Jan 01 2016 16:41:37 GMT-0800 (PST)",
    "pushid5": "Fri Jan 01 2016 16:42:04 GMT-0800 (PST)"
  },
  "index": {
    "pushid1": true,
    "pushid3": true,
    "pushid5": true
  }
}

So we store strings representing date/times and have an index to select a subset of these items.

We can now load the nodes from the index, then load the items that those nodes refer to and display them in the views with:

FirebaseRecyclerViewAdapter<Boolean, ItemViewHolder> adapter = 
    new FirebaseRecyclerViewAdapter<Boolean, ItemViewHolder>(
        Boolean.class, android.R.layout.two_line_list_item, ItemViewHolder.class, ref.child("index")){
    protected void populateViewHolder(final ItemViewHolder viewHolder, Boolean model, int position) {
        String key = this.getRef(position).getKey();
        ref.child("items").child(key).addListenerForSingleValueEvent(new ValueEventListener() {
            public void onDataChange(DataSnapshot dataSnapshot) {
                String date = dataSnapshot.getValue(String.class);
                ((TextView)viewHolder.itemView.findViewById(android.R.id.text1)).setText(date);
            }

            public void onCancelled(FirebaseError firebaseError) { }
        });
    }
};

The output on screen:

For the full code, see Activity34559171 in this repo.

这篇关于将 FirebaseRecyclerViewAdapter 耦合到布尔/字符串 Map.Entry的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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