【问题标题】:Display data from SQLite as dynamic cards with Android Studio使用 Android Studio 将 SQLite 中的数据显示为动态卡片
【发布时间】:2020-05-30 16:16:10
【问题描述】:

现在我从我的数据库中获取用户名一个字符串数组。我想以这样的布局显示它:enter image description here

应该是动态的,如果名字很长,每行只有一个名字,...有时20个名字,有时只有一个名字,所以需要灵活。我是 Android Studio 的新手,不确定使用列表视图甚至文本视图是否有意义?或者也许是别的什么?

提前非常感谢您!

【问题讨论】:

    标签: android android-studio android-layout android-listview android-sqlite


    【解决方案1】:

    使用带有 Flexbox 布局管理器的 Recyclerview https://github.com/google/flexbox-layout

    设置了 flexWrap。

    FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(context);
            layoutManager.setFlexWrap(FlexWrap.WRAP);
            RecyclerView.setLayoutManager(layoutManager);
    
            // Create Custom RecyclerAdapter for data
            RecyclerViewAdapter adapter = new CustomAdapter(context,stringArrayList);
            RecyclerView.setAdapter(adapter);
    

    RecyclerView 类似于 listview 但性能更好。

    使用 ArrayList 而不是数组,因为这样更灵活。

    在适配器中,每个项目都在onBinViewHolder 中“绘制”,您可以为每个名称使用一个文本框,或者您的示例看起来像是使用芯片。

    有关 RecyclerView 的更多详细信息,请访问https://developer.android.com/guide/topics/ui/layout/recyclerview

    【讨论】:

      猜你喜欢
      • 2021-10-02
      • 2017-12-27
      • 2020-01-27
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      相关资源
      最近更新 更多