i'm trying to fetch data from mysql database using volley and display it in recycleview but i've a problem the recycleview is not shwoing. here is 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")
}
}
CustomAdapter.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
}
}
}
Order.kt
package com.example.demo
data class Order(val id: Int, val orderDate: String, val status: String, val clientName: String)
i tried to change in the layout of the activiy_main.xml and order_layout.xml but nothing worked and also tried to log to know if the data is fetched and i found out that they are retrieved
You are not updating the adapter anywhere.
After you add things to ordersList
you need to let the adapter for the RecyclerView
know.
e.g.:
fun update(val newOrdersList: List<Order>) {
ordersList.clearAll()
ordersList.addAll(newOrdersList)
notifyDataSetChanged() // This is deprecated but works - demonstration only
}
The whole notifyDatasetChanged
might send you down a new rabbit hole of optimizing the performance of the RecyclerView
but to give you a simple answer to your problem I won't go into it.