我有一个活动,我试图从API中获取数据,并在那里显示列表,但我无法在
adapter
.
代码
MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
categories.layoutManager = LinearLayoutManager(this)
categories.adapter = CategoriesAdapter(//what should be here?)
}
// api code
private fun callAPIDemo(textView: TextView) {
// Instantiate the RequestQueue.
val queue = Volley.newRequestQueue(this)
val url = "https://example.com/api/isp/categories"
// Request a string response from the provided URL.
val stringRequest = StringRequest(
Request.Method.GET, url,
Response.Listener<String> { response ->
val jsonArray = JSONArray(response)
val list: MutableList<Category> = ArrayList()
for (i in 0 until jsonArray.length()) {
val jsonObject = jsonArray.getJSONObject(i)
list.add(parseData(jsonObject))
}
// here you will have the complete list of data in your "list" variable
},
Response.ErrorListener { textView.text = "That didn't work!" })
// Add the request to the RequestQueue.
queue.add(stringRequest)
}
// parsing data
private fun parseData(jsonObject: JSONObject): Category {
var listingObject = Category(
jsonObject.getString("name"),
jsonObject.getString("slug"),
jsonObject.getString("image")
)
return listingObject
}
//
CategoriesAdapter.kt
class CategoriesAdapter(val categories : ArrayList<Category>) : RecyclerView.Adapter<CategoriesAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view: View = LayoutInflater.from(parent.context).inflate(R.layout.categories_row, parent, false)
return ViewHolder(view)
}
override fun getItemCount(): Int {
return categories.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.textView.text = categories.get(position).name
holder.textViewSlug.text = categories.get(position).slug
//hardcoding the image, just for simplicity, you can set this also from data list same as above
//holder.aImage.setImageResource(R.mipmap.ic_launcher)
//here is the image setup by using glide
Glide.with(holder.aImage.context).load(categories.get(position).image).into(holder.aImage)
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var textView: TextView
var textViewSlug: TextView
var aImage: ImageView
init {
textViewSlug = itemView.findViewById(R.id.text_slug)
textView = itemView.findViewById(R.id.text_name)
aImage = itemView.findViewById(R.id.a_image)
}
}
}
Category.kt
data class Category(
val name: String,
val slug: String,
val image: String
)
activity_main.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"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/categories"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
categries_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<ImageView
android:id="@+id/a_image"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="80dp"
android:layout_height="80dp"
android:contentDescription="@string/image" />
<TextView
android:id="@+id/text_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@+id/a_image"
app:layout_constraintStart_toEndOf="@+id/a_image"
app:layout_constraintTop_toTopOf="@+id/a_image" />
<TextView
android:id="@+id/text_slug"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@+id/text_name"
app:layout_constraintStart_toEndOf="@+id/text_name"
app:layout_constraintTop_toTopOf="@+id/text_name" />
</LinearLayout>
我应该放什么
categories.adapter = CategoriesAdapter(//what should be here?)
主要活动.kt