将Firebase列表适配器与Card View配合使用

时间:2016-08-18 21:53:13

标签: android firebase firebase-realtime-database firebaseui cardview

我的firebase列表适配器如下

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    mListview = (ListView) findViewById(R.id.listview);

    mAuth = FirebaseAuth.getInstance();
    //the post list shit is happening here

    DatabaseReference root = FirebaseDatabase.getInstance().getReference();
    FirebaseUser user = mAuth.getCurrentUser();
    DatabaseReference postRef = root.child("users").child(user.getUid().toString()).child("posts");

    ListAdapter adapter = new FirebaseListAdapter<PostList>(this,PostList.class,android.R.layout.simple_list_item_1,postRef) {
        @Override
        protected void populateView(View view, PostList postList, int i) {
            TextView text = (TextView)view.findViewById(android.R.id.text1);
            text.setText(postList.getBody());

        }

    };
    mListview.setAdapter(adapter);

我有一个带有列表视图的普通xml和另一个带有卡片视图的xml

我的问题是我找不到将我的cardview xml文件与我的活动相关联的方法,所以我的列表只是一直显示为普通列表, 在初始化firebase列表适配器时,我找到了R.layout_simple_list_item 我不确定究竟是如何工作的,我用谷歌搜索,没有什么能解释为什么我不能简单地说R_layout_my_card_view_xml。另外,如果我发现这一切都错了,请告诉我。

1 个答案:

答案 0 :(得分:0)

我已经得到了答案,以防万一有人遇到同样的问题,正如Linxy明确指出的那样,最好使用FirebaseRecyclerAdapter,它是{{1}的修改版本}。

但是,如果您仍想使用ListAdapter,那么:

  1. 首先请注意,您无法直接编辑ListAdapter,而是创建自己的布局,假设您在布局文件中有row.xml,请使用android.R.layout.items作为R.layout.row参数而不是populateView(),后者不会找到任何东西,因为那些布局文件存储在你的SDK文件夹中。

  2. 请考虑使用回收器适配器,不是那么难。

相关问题