运行应用程序时不显示RecycleView

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

我正在尝试使用 volley 从 mysql 数据库获取数据并将其显示在 recycleview 中,但我有一个问题,recycleview 没有显示。 这是 order_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"
    android:id="@+id/cardHolder"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:background="@drawable/order_background"
    android:padding="8dp">

    <LinearLayout
        android:id="@+id/orderView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:background="@drawable/orderid_background"
        android:orientation="vertical"
        android:padding="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Order ID "
            android:textColor="@color/white" />

        <TextView
            android:id="@+id/orderID"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="6dp"
            android:text="1"
            android:textColor="@color/white"
            android:textSize="18sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/orderView"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Client Name:"
            android:textColor="@color/black"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/clientName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:text="test"
            android:textColor="@color/black"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/orderView"
        app:layout_constraintTop_toBottomOf="@+id/linear1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Order Date:"
            android:textColor="@color/black"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/orderDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:text="test"
            android:textColor="@color/black"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/orderView"
        app:layout_constraintTop_toBottomOf="@+id/linear2">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Status:"
            android:textColor="@color/black"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/orderStatus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:text="test"
            android:textColor="@color/black"
            android:textSize="20sp" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">


        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/RecycleV"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:itemCount="2"
            tools:listitem="@layout/order_layout" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/addBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_margin="16dp"
            android:backgroundTint="@color/cardview_dark_background"
            android:contentDescription="@string/adding_order"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:srcCompat="@drawable/baseline_add_24"
            app:tint="@color/white" />


    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

MainActivity.kt

package com.example.demo

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.android.volley.Request
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.example.demo.databinding.ActivityMainBinding
import org.json.JSONArray
import org.json.JSONException

class MainActivity : AppCompatActivity() {
    private val URL = "http://192.168.100.12/demo/Android/v1/getAll.php"
    lateinit var binding: ActivityMainBinding
    private lateinit var ordersList: ArrayList<Order>
    private lateinit var rcvAdapter: CustomAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        binding = ActivityMainBinding.inflate(layoutInflater)
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        ordersList = ArrayList()
        rcvAdapter = CustomAdapter(ordersList)
        binding.RecycleV.layoutManager = LinearLayoutManager(this)
        binding.RecycleV.setHasFixedSize(true)
        binding.RecycleV.adapter = rcvAdapter
        loadData()
//        binding.rv.apply {
//            layoutManager = LinearLayoutManager(this@MainActivity)
//            adapter = rcvAdapter
//        }
//        binding.sr.setOnRefreshListener {
//            loadData()
//            binding.sr.isRefreshing = false
//        }
//        binding.addBtn.setOnClickListener {
//            val intent = Intent()
//        }
    }

    private fun loadData() {
        val queue = Volley.newRequestQueue(this)
        val stringRequest = StringRequest(Request.Method.POST, URL, { response ->
            try {
                Log.e("anytext", response)
                val jsonArray = JSONArray(response)
                for (i: Int in 0 until jsonArray.length()) {
                    val jsonObject = jsonArray.getJSONObject(i)
                    val order = Order(
                        jsonObject.getInt("ID"),
                        jsonObject.getString("orderDate"),
                        jsonObject.getString("status"),
                        jsonObject.getString("name")
                    )
                    ordersList.add(order)
                    Log.e("order data", order.toString())
                    Log.e("array size", ordersList.size.toString())

                }
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            if (response.isEmpty()) {
                Toast.makeText(this, "There is no orders", Toast.LENGTH_SHORT).show()
            }

        }, { error ->
            Toast.makeText(
                this, error.message, Toast.LENGTH_LONG
            ).show()
        })
        queue.add(stringRequest)
        Log.e("final", "success")
    }
}

自定义适配器.kt

package com.example.demo

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class CustomAdapter(
    private val ordersList: ArrayList<Order>
) : RecyclerView.Adapter<CustomAdapter.MyViewHolder>() {

    inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val id: TextView = itemView.findViewById(R.id.orderID)
        val clientName: TextView = itemView.findViewById(R.id.clientName)
        val orderDate: TextView = itemView.findViewById(R.id.orderDate)
        val orderStatus: TextView = itemView.findViewById(R.id.orderStatus)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.order_layout, parent, false)
        return MyViewHolder(view)
    }

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

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val order = ordersList[position]
        holder.apply {
            id.text = order.id.toString()
            orderStatus.text = order.status
            orderDate.text = order.orderDate
            clientName.text = order.clientName
        }
    }
}

订单.kt

package com.example.demo

data class Order(val id: Int, val orderDate: String, val status: String, val clientName: String)

我尝试更改 activiy_main.xml 和 order_layout.xml 的布局,但没有任何效果,并且还尝试记录以了解数据是否已获取,我发现它们已被检索

android kotlin android-recyclerview android-volley
1个回答
0
投票

您没有在任何地方更新适配器。

ordersList
添加内容后,您需要让
RecyclerView
的适配器知道。

例如:

fun update(val newOrdersList: List<Order>) {
    ordersList.clearAll()
    ordersList.addAll(newOrdersList)
    notifyDataSetChanged() // This is deprecated but works - demonstration only
}

整个

notifyDatasetChanged
可能会给您带来一个优化
RecyclerView
性能的新兔子洞,但为了给您一个简单的答案,我不会深入讨论它。

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