【问题标题】:Recyclerview only showing first valueRecyclerview 仅显示第一个值
【发布时间】:2018-08-07 00:05:06
【问题描述】:

我的 gson 解析有问题。我有一个嵌套的 gson,我想在属性下获取“名称”的值。问题是我的 recyclerview 只显示第一个值(即账单 1)而不是账单 2 或账单 3。这是我的代码

// MainActivity.kt
    recycler_view.layoutManager = LinearLayoutManager(requireContext())
    recycler_view.addItemDecoration(LineItemDecorator(requireContext()))
    val bill = GsonBuilder().create().fromJson(jsonString, Bills::class.java)
            val nameList: List<Bill> = Arrays.asList(bill)
            val adapter = BillsRecyclerAdapter(nameList)
            recycler_view.adapter = adapter
            adapter.notifyDataSetChanged()

// Bills.kt
    data class Bills(
        val data: ArrayList<Data>,
        val meta: Meta,
        val links: Links
    )

// BillsRecyclerAdapter.kt
    class BillsRecyclerAdapter(private val items: List<Bill>):
            RecyclerView.Adapter<BillsRecyclerAdapter.BillsHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BillsHolder {
            return BillsHolder(parent.inflate(R.layout.bills_list_item))
        }

        override fun onBindViewHolder(holder: BillsHolder, position: Int) {
            holder.billName.text = items[position].data[position].attributes.name       
        }


        override fun getItemCount() = items.size

        class BillsHolder(view: View): RecyclerView.ViewHolder(view) {
            val billName: TextView = view.billName
            val billAmount: TextView = view.billAmount
        }
    }

<!-- recyclerview.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">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_marginTop="@dimen/materialize_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

<!-- bill_list_item.xml-->
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/billName"
        android:textSize="20sp"
        android:layout_marginStart="8dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"/>

    <TextView
        android:id="@+id/billAmount"
        android:textSize="20sp"
        android:layout_marginEnd="8dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

这是我的 json 字符串。 https://pastebin.com/iEqhmB35

感谢您提供的任何帮助

【问题讨论】:

  • 您能否发布您的 XML 文件。我想我知道答案了。
  • 我已经编辑了我的帖子

标签: android json android-recyclerview kotlin


【解决方案1】:

检查以下

  1. 检查你的items.size,如果它真的不为零,
  2. 同时检查您的bills_list_item.xml 尝试将根父级的高度设置为wrap_content
  3. 还要检查你是否有这个recyclerview

    recyclerView.setHasFixedSize(true);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    
  4. 重要的是布局管理器

如果没有帮助,能否分享一下你的Bills.java 类,JSON 原始数据的序列化可能有问题。

希望这会有所帮助。 干杯

【讨论】:

  • 我的 Bills.java 在 OP 中。我的 items.size() 是 1(这是不对的)
  • 我认为你做得很好,Bill.java 类保存数据数组有什么问题。所以不要使用 Bill 作为列表,而是使用 Bill.data.size() 检查你的 json,它解析数据数组对象并将账单保存为一个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-07
  • 1970-01-01
  • 1970-01-01
  • 2021-11-02
相关资源
最近更新 更多