Home > Mobile >  How to pass from recyclerview fragment to another recyclerview fragment
How to pass from recyclerview fragment to another recyclerview fragment

Time:11-19

Can someone help me on how to transfer data from one recyclerview fragment to another recyclerview fragment?

This is my newly created CartRecyclerAdapter.kt for my cart recyclerview from a fragment. The main idea of SubmitItem() is to accept the selected item in the Itemrecyclerview.

package com.example.karawapplication
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.example.karawapplication.models.ItemPost
import kotlinx.android.synthetic.main.layout_item_cart_list.view.*
import kotlinx.android.synthetic.main.layout_item_list_item.view.*

class CartRecyclerAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private var Items : List<ItemPost> = emptyList()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return ItemRecyclerAdapter.ItemViewHolder(
            LayoutInflater.from(parent.context)
                .inflate(R.layout.layout_item_cart_list, parent, false)
        )
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when(holder)
        {
            is ItemRecyclerAdapter.ItemViewHolder ->
            {
                holder.bind(Items.get(position))
            }
        }
    }

    override fun getItemCount(): Int {
        return Items.size
    }
    fun SubmitItem(Item : ItemPost)
    {
        Items.toMutableList().add(Item)
    }
    class ItemViewHolder constructor(
        Itemview : View
    ): RecyclerView.ViewHolder(Itemview)
    {
        val ItemImage : ImageView = Itemview.Item_image_cart
        val ItemTitle : TextView = Itemview.Item_title_cart
        val ItemCategory: TextView = Itemview.Item_category_cart
        val ItemPrice : TextView = Itemview.Item_price_cart
        fun bind (itempost : ItemPost)
        {
            ItemTitle.setText(itempost.Itemtitle)
            ItemCategory.setText(itempost.ItemCategory)
            ItemPrice.setText(itempost.ItemPrice)
            val requestOptions = RequestOptions()
                .placeholder(R.drawable.ic_baseline_brush_24)
                .error(R.drawable.ic_baseline_brush_24)

            Glide.with(itemView.getContext())
                .applyDefaultRequestOptions(requestOptions)
                .load(itempost.image)
                .into(ItemImage)
        }

    }
}

This is where I use the SubmitItem() in my ItemRecyclerAdapter.kt which contains items of my shop app and displayed from another fragment. CartAdapter is the adapter of the cart that I recently created in order to access the function SubmitItem().

        ItemButton.setOnClickListener()
        {
            Toast.makeText(itemView.context, "${itempost.image}, ${itempost.Itemtitle}, ${itempost.ItemPrice}", Toast.LENGTH_SHORT).show()
            CartAdapter.SubmitItem(ItemPost(itempost.image,itempost.Itemtitle,itempost.ItemCategory,itempost.ItemPrice))
        }

This is my code for my fragments

ShopFragment.kt contains the recyclerview of my items.

package com.example.karawapplication.fragments

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.android.volley.Request
import com.android.volley.VolleyError
import com.android.volley.toolbox.JsonArrayRequest
import com.android.volley.toolbox.Volley
import com.example.karawapplication.ItemRecyclerAdapter
import com.example.karawapplication.R
import com.example.karawapplication.models.ItemPost
import kotlinx.android.synthetic.main.fragment_shop.*
import org.json.JSONException
import org.json.JSONObject

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [Shop.newInstance] factory method to
 * create an instance of this fragment.
 */
class Shop : Fragment(){
    // TODO: Rename and change types of parameters
    private lateinit var ItemAdapter : ItemRecyclerAdapter
    private var param1: String? = null
    private var param2: String? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }

    }
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_shop, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initRecyclerView()
        addDataSet()

    }
    // CUSTOM FUNCTIONS
    private fun addDataSet()
    {
        createDataset {
            list -> ItemAdapter.SubmitList(list)
        }
    }
    private fun initRecyclerView()
    {
        ShopRecycleView.layoutManager = LinearLayoutManager(activity)
        ItemAdapter = ItemRecyclerAdapter()
        ShopRecycleView.adapter = ItemAdapter
        //Toast.makeText(context, "Recycler Trigger", Toast.LENGTH_SHORT).show()
    }
     // https://tutorials.eu/json-parsing-and-how-to-use-gson-in-android/
    // Generates the data for the recycleview
    fun createDataset(onSuccess: (List<ItemPost>) -> Unit){
         val url = "http://api.karawcraftventure.com/item"
         // LIST DATA VARIABLE FOR RECYCLEVIEW
         val list = ArrayList<ItemPost>()
         // VOLLEY API REQUEST
         val Queue = Volley.newRequestQueue(activity)
         val jsonObject = JsonArrayRequest(
             Request.Method.GET,url,null,
             {response ->
                 try
                 {
                     for (i in 0 until response.length())
                     {
                         val item : JSONObject = response.getJSONObject(i)
                         val API_Image : String = item.getString("product_url_image")
                         val API_ItemName : String = item.getString("product_name")
                         val API_Price : String = item.getString("product_price")
                         val API_Category : String = item.getString("product_category")
                         // Toast Notif if data is extracted or not
                         //Toast.makeText(context, "$API_ItemName - $API_Price - $API_Category", Toast.LENGTH_SHORT).show()
                         list.add(ItemPost(API_Image, API_ItemName, API_Category, API_Price))
                     }
                     onSuccess(list)
                 }
                 catch (e: JSONException)
                 {
                     e.printStackTrace()
                 }
             },
             { error: VolleyError? -> Toast.makeText(context, error?.message.toString(), Toast.LENGTH_SHORT).show()

             }
         )
         Queue.add(jsonObject)
    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment Shop.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic

        fun newInstance(param1: String, param2: String) =
            Shop().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}

Cart.kt fragment on the other hand, contains my shopping cart.

package com.example.karawapplication.fragments

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.karawapplication.CartRecyclerAdapter
import com.example.karawapplication.ItemRecyclerAdapter
import com.example.karawapplication.R
import kotlinx.android.synthetic.main.fragment_cart.*
import kotlinx.android.synthetic.main.fragment_shop.*
import kotlinx.android.synthetic.main.fragment_shop.ShopRecycleView

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [Cart.newInstance] factory method to
 * create an instance of this fragment.
 */
class Cart : Fragment() {
    // TODO: Rename and change types of parameters
    private lateinit var ItemAdapter : CartRecyclerAdapter
    private var param1: String? = null
    private var param2: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initRecyclerView()
    }
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_cart, container, false)
    }
    private fun initRecyclerView()
    {
        Cart_RecyclerView.layoutManager = LinearLayoutManager(activity)
        ItemAdapter = CartRecyclerAdapter()
        Cart_RecyclerView.adapter = ItemAdapter
        //Toast.makeText(context, "Recycler Trigger", Toast.LENGTH_SHORT).show()
    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment Cart.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            Cart().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}

The code has no errors, but it does not show any output in my cart recyclerview fragment.

CodePudding user response:

(I'm going to follow the "starts with a lowercase letter" convention for variables and functions here, because I'm tired and it's less confusing)

Your submitItem function just creates a new copy of items (items.toMutableList()) and adds to that. Then it's immediately discarded when you exit the function.

All your adapter code that handles your data (getItemCount, onBindViewHolder) references items, so you need to update that instead, and let the adapter know it's changed:

fun submitItem(item: ItemPost) {
    // plus creates a copy, but adds the new item without making the result mutable
    items = items.plus(item)
    // we also need to tell the adapter that the data has changed, so it can
    // update - there are more efficient calls (you'll probably get a lint
    // warning telling you to use one) but this is the simplest
    notifyDataSetChanged()
}

This is just making your adapter update correctly - I'm assuming you've got your communication between the two fragments set up properly (communicating through the parent activity, passing one fragment's adapter into the other, using a view model). Have a read of this if you need to: Communicating with fragments

  • Related