Kotlin recycle view and custom adapter is creating only one row

问题描述 投票:0回答:1

I have an issue with my custom adapter (I think) I'm getting my data using retrofit, that comes just fine, once I get the data I call another function to populate my recycle view, as follows.

I really appreciate if some one could help me on this...

    private fun createMyOrdersTable(myOrders: List<OrderAssigned>){
        val ordersCount = myOrders.size
        val rv = myorder_rv
        rv.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
        val ordersList = ArrayList<OrderAssigned>()
        if (ordersCount > 0){
            myOrders.forEach {
                ordersList.add(it)
            }
//            order.add(OrderAssigned("123","123213","123123","1231231","123123","123123","123123","123123"))
//            order.add(OrderAssigned("123","123213","123123","1231231","123123","123123","123123","123123"))
            println(ordersList)
            val adapter = CustomAdapterMyOrders(ordersList)
            rv.adapter = adapter
        }else{
            Toast.makeText(context, "You don't have any assigned order", Toast.LENGTH_LONG).show()
        }
    }

And this is my adapter:

package com.davesm.swiftdemexico

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.davesm.swiftdemexico.Model.OrderAssigned
import kotlinx.android.synthetic.main.myorders_layout.view.*

class CustomAdapterMyOrders(private val orders: ArrayList<OrderAssigned>): RecyclerView.Adapter<CustomAdapterMyOrders.ViewHolder>() {

    inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
        val orderID = itemView.orderID!!
        val clientName =  itemView.clientName!!
        val no_rem = itemView.orderRem!!
        val orderDate = itemView.orderDate!!
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        //
        val v = LayoutInflater.from(parent?.context).inflate(R.layout.myorders_layout, parent, false)
        return ViewHolder(v);
    }

    override fun getItemCount(): Int {
        //
        return orders.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        val model = orders[position]
        holder.orderID.text = "ID: "+model.id
        holder.clientName.text = model.nom_cte
        holder.no_rem.text = "Remisión"+model.no_rem
        holder.orderDate.text = model.falta_rem
    }
}

When I execute my app it shows only one "row" which is a card view even when I add more than one item in the foreach in the "createMyOrdersTable" I had tried to add it manually as in the commented code but still not working, I send the data as an ArrayList.

Here is the xml files for the views:

myorder_layout.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/myorder_rv"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="1dp"
        android:layout_marginBottom="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

This is my items layout.myorders_layout.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="10dp"
        app:cardCornerRadius="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <LinearLayout
            android:padding="10dp"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/rounder_button_primary_color"
            tools:ignore="MissingConstraints">

            <TextView
                android:id="@+id/orderID"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Order ID"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />
            <TextView
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
                android:text="Client Name"
                android:id="@+id/clientName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <TextView
                android:id="@+id/orderRem"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Remisión" />

            <TextView
                android:id="@+id/orderDate"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Date" />
        </LinearLayout>
    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
android kotlin cardview
1个回答
1
投票

It is not creating 1 row, it is creating many, but they are all as tall as the screen.

Change your item layout to use wrap_content for the height

© www.soinside.com 2019 - 2024. All rights reserved.