使用FirebaseListAdapter填充ExpandableListView

时间:2016-06-20 06:13:31

标签: android listview firebase expandablelistview firebase-realtime-database

我在过去的4个月里一直在与Firebase合作,我找到了一个我非常欣赏一些洞察力的话题。 现在我很高兴在Android应用程序中使用FirebaseListAdapter用于标准ListViews,但我想增强我的应用程序以使用ExpandableListViews。

方案:
我向应用程序上的开放社区发布请求,当人们查看时,他们可以响应在2个用户之间创建私人对话。我想显示ExpandableListView,将组作为请求中的信息,每个子节点都是关联的对话。

Fragment_Message_History.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="18">

    <TextView
        android:id="@+id/unresolved_text"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/unresolved_string"
        android:textStyle="bold"
        android:paddingRight="10dp"
        android:paddingLeft="10dp"
        android:background="@drawable/action_bar_bg"/>

    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/unresolvedListView"
        android:layout_gravity="center_horizontal"
        android:layout_weight="8"/>

</LinearLayout>

MessageHistoryFragment

public class MessageHistoryFragment extends Fragment {

    ExpandableListView unresolvedList;
    ExpandableListView resolvedList;
    boolean truth = false;
    private Firebase mFirebaseConvoRef;
    private Firebase mFirebasePostRef;
    FirebaseListAdapter<PostRequest> unresolvedAdapter;
    FirebaseListAdapter<ConversationClass> resolvedAdapter;
    private Firebase userAuth;
    private Query unresolvedQuery;

    public static MessageHistoryFragment newInstance(){return new MessageHistoryFragment();}

    public MessageHistoryFragment(){

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_message_history, container, false);
        unresolvedList = (ExpandableListView) view.findViewById(R.id.unresolvedListView);
        resolvedList = (ExpandableListView) view.findViewById(R.id.resolvedListView);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        truth = true;
        super.onCreate(savedInstanceState);
        mFirebaseConvoRef = new Firebase("https://<id>.firebaseio.com/Conversations/");
        mFirebasePostRef  = new Firebase("https://<id>.firebaseio.com/Requests/");

        unresolvedAdapter = new FirebaseListAdapter<PostRequest>(getActivity(),PostRequest.class,
                R.layout.group_item_unresolved, unresolvedQuery) {
            @Override
            protected void populateView(View view, PostRequest postRequest, int i) {
                ((TextView)view.findViewById(R.id.request_history_text))
                        .setText(postRequest.getRequest());
            }
        };
        unresolvedList.setAdapter(unresolvedAdapter);


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}

数据库结构

Conversations:{
    convoID1:{
        postId:"requestID1"
        user1:"me"
        user2:"you"
    }
    convoID2:{
        postId:"requestID1"
        user1:"me"
        user2: "someone else"
   }
   convoID3:{ ... }
 }
 Messages:{
    convoID1:{ ...messages}
    convoID2:{ ...messages}
 }
 Requests:{
    requestID1: {
        request:
        details:
        requester:
        timestamp:
    }
    requestID2: {...}
    requestID3: {...}
 }

此时我觉得远程接近完成此目标的方法将违反Firebase数据库结构建议,以使结构变平并将对话映射为请求的子项。

有没有人知道如何填充ExpandableListView? 感谢一堆世界!

0 个答案:

没有答案