Can someone please check this and help me? I have a textchange listener in the adapter, and api call in fragment. whenever there is text change, data is fetched from API, and item is replaced. Now the item is being carry forward in run mode, for example. When i change item b from item a, item a is only shown, when i change it to item c, item b is shown, when i change it to item d, item c is shown. It works fine in debug mode.
my fragment:
class upload_invent : Fragment() {
var modelclass=ModelClass()
lateinit var v:View
private var _binding: FragmentUploadInventBinding? = null
private val binding get() = _binding!!
private val recyclerViewAdapteru: RecyclerViewAdapterU by lazy {
RecyclerViewAdapterU(RecyclerViewAdapter.ob.dataSelected)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
@SuppressLint("WrongConstant")
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
v= inflater.inflate(R.layout.fragment_upload_invent,container,false)
_binding= FragmentUploadInventBinding.bind(v)
binding.recyclerViewU.apply {
layoutManager=LinearLayoutManager(activity)
adapter = recyclerViewAdapteru
}
binding.searchButton.setOnClickListener{
v.findNavController().navigate(R.id.action_upload_invent_to_search_invent2)
}
return binding.root
}
fun updateStockDetail(skuCode:String,pos: Int){
var skuDetails:ArrayList<ModelClass> = arrayListOf()
val call: Call<List<ModelClass>>? =
ApiClient.instance?.myApi?.getfromsku(skuCode)!!
call!!.enqueue(object : Callback<List<ModelClass>?> {
override fun onResponse(
call: Call<List<ModelClass>?>,
response: Response<List<ModelClass>?>
) {
if (response.body()!=null){
skuDetails =response.body() as ArrayList<ModelClass>}
val x = RecyclerViewAdapter.ob.dataSelected[pos]
//RecyclerViewAdapter.ob.dataSelected.set(pos,skuDetails[0])
println(RecyclerViewAdapter.ob.dataSelected[pos].item_name)
println(x.sku_code)
recyclerViewAdapteru.setData(skuDetails,pos)
}
override fun onFailure(call: Call<List<ModelClass>?>, t: Throwable) {
}
})
}
}
This is my adapter:
class RecyclerViewAdapterU (var dataList:ArrayList<ModelClass>): RecyclerView.Adapter<RecyclerViewAdapterU.ViewHolder>() {
var _binding: UploadItemViewBinding? = null
val binding get() = _binding!!
lateinit var v:View
var obj:upload_invent = upload_invent()
fun setData(listModel: ArrayList<ModelClass>,pos:Int) {
dataList[pos]=listModel[0]
notifyItemChanged(pos)
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): RecyclerViewAdapterU.ViewHolder {
v = LayoutInflater.from(parent.context).inflate(R.layout.upload_item_view, parent, false)
_binding = UploadItemViewBinding.bind(v)
return ViewHolder(binding.root)
}
override fun onBindViewHolder(holder: ViewHolder, @SuppressLint("RecyclerView") position: Int) {
holder.bindItems(dataList[position])
}
override fun getItemCount(): Int {
return dataList.size
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(data: ModelClass) {
binding.apply {
itemquant.text=data.item_quant
uploadItemName.text = data.item_name
uploadMfg.text = data.mfg
skuStock.setText(data.item_stock.toString())
skuCode.setText(data.sku_code)
}
updateStockDetail()
}
fun updateStockDetail(){
binding.skuCode.addTextChangedListener(object : TextWatcher{
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
}
override fun afterTextChanged(editable: Editable) {
if (editable.length>=3){
//binding.skuCode.removeTextChangedListener(this)
var pos:Int=adapterPosition
var x= editable.toString().trim()
println("$x in textwatcher and $pos")
obj.updateStockDetail(editable.toString(),pos)
//notifyDataSetChanged()
//dataList[pos]=RecyclerViewAdapter.ob.dataSelected[pos]
notifyItemChanged(pos)
v.findNavController().navigate(R.id.action_upload_invent_refresh)
}}
})
}
}
}
note:ob.dataselected is from another recyclerview.