Reputation: 13
hi i want parse my data in to new activity . I did this but it doesn't work properly and it doesn't transfer data You can see my code below
my adapter ItemViewAdapter.kt
package com.example.app.adapter
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.app.R
import com.example.app.models.products.Results
import com.example.app.ui.home.DescriptionActivity
import java.security.AccessController.getContext
class ItemViewAdapter(private val context: Context) :
RecyclerView.Adapter<ItemViewAdapter.MyViewHolder>() {
private var itemList: MutableList<Results> = mutableListOf()
private var page: Int = 0
fun setListItem(item: List<Results>) {
itemList.addAll(item)
notifyItemRangeInserted(page * 10, (page.plus(1) * 10) - 1)
}
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
private val title = view.findViewById<TextView>(R.id.tvTitle)
private val code = view.findViewById<TextView>(R.id.tvNumber)
private val quantity = view.findViewById<TextView>(R.id.tvQuantity)
private val price = view.findViewById<TextView>(R.id.tvPrice)
private val available = view.findViewById<TextView>(R.id.tvAvailable)
private val category = view.findViewById<TextView>(R.id.tvCategory)
fun bind(item: Results) {
title.text = item.name
code.text = item.code.toString()
quantity.text = item.quantities[0].quantity.toString()
price.text = item.prices[0].price.toString()
available.text = item.active.toString()
category.text = item.category?.name
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.list_item, parent, false)
return MyViewHolder(v)
}
override fun getItemCount(): Int {
return itemList.size
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val data = itemList[position]
holder.bind(data)
holder.itemView.setOnClickListener {
val intent = Intent(context, DescriptionActivity::class.java)
intent.putExtra("title", getItemId(position))
intent.putExtra("code", getItemId(position))
intent.putExtra("quantity", getItemId(position))
intent.putExtra("price", getItemId(position))
intent.putExtra("category", getItemId(position))
intent.putExtra("info", getItemId(position))
context.startActivity(intent)
}
}
}
and my description activity
Description.kt
package com.example.app.ui.home
import android.graphics.Color
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.WindowInsetsController
import android.view.WindowManager
import com.example.app.R
import com.example.app.databinding.ActivityDescriptionBinding
@Suppress("DEPRECATION")
class DescriptionActivity : AppCompatActivity() {
private lateinit var binding: ActivityDescriptionBinding
override fun onCreate(savedInstanceState: Bundle?) {
window.apply {
clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
statusBarColor = Color.TRANSPARENT
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
[email protected]
?.setSystemBarsAppearance(
0,
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
)
}
}
}
super.onCreate(savedInstanceState)
binding = ActivityDescriptionBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
getItemIntent()
}
private fun getItemIntent(){
val title = intent.getStringExtra("title")
val code = intent.getStringExtra("code")
val category = intent.getStringExtra("category")
val info = intent.getStringExtra("info")
val price = intent.getStringExtra("price")
val quantity = intent.getStringExtra("quantity")
setData(title,code,category,info,price,quantity)
}
private fun setData(title: String?, code: String?, category: String?, info: String?, price: String?, quantity: String?) {
binding.tvTitle.setText(title)
binding.tvCode.setText(code)
binding.tvCategory.setText(category)
binding.tvDescription.setText(info)
binding.tvPrice.setText(price)
binding.tvQuantity.setText(quantity)
}
}
But the data is not transferred. If anyone can help me improve the code or offer me a sample code done with api so I can see.
Upvotes: 0
Views: 98
Reputation: 188
If you need to pass the entire object or most of its properties, you can add the library
implementation 'com.google.code.gson:gson:2.8.5' //Maybe the version is not the most recent, I just copied and pasted the implementation from the internet
And convert your object to a Json and pass it as a string
intent.putExtra("data", Gson().toJson(data))
and to receive it
val data = intent.getStringExtra("data", null)?.let {
Gson().fromJson(it, Results::Java.class)
}
Upvotes: 0
Reputation: 10910
When you set the data, you call getItemId(position)
for every single line. This returns a long
, not a string (and you put the same long
for each attribute, which makes no sense)
intent.putExtra("title", getItemId(position))
intent.putExtra("code", getItemId(position))
but when you retrieve it, you look for a string
val title = intent.getStringExtra("title")
val code = intent.getStringExtra("code")
If you want to get a string, you need to put a string, probably like this (get the item for that row, then put its string attributes):
val data = itemList[position]
//...
intent.putExtra("title", data.name)
intent.putExtra("code", data.code.toString())
or if you want to get the text off the displayed views (have to make them non-private in the binding class):
intent.putExtra("title", binding.title.text.toString())
intent.putExtra("code", binding.code.text.toString())
Upvotes: 2