将 Firebase 列表适配器与卡片视图结合使用

Use Firebase list adapter with Card View

我的 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 文件关联到我的 activity,所以我的列表一直显示为普通列表, 在初始化 firebase 列表适配器时,我发现了一个 R.layout_simple_list_item 我不确定它到底是如何工作的,我已经用谷歌搜索了,没有任何东西可以明确解释为什么我不能只说 R_layout_my_card_view_xml。另外,如果我做错了,请告诉我。

我已经得到了答案,以防万一有人遇到同样的问题,就像 Linxy 明确指出的那样,最好使用 FirebaseRecyclerAdapter,它是 [= 的修改版本11=].

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

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

  2. 请考虑回收器适配器,它并不难。