Home > Blockchain >  What context should I pass to the AdapterItem from InformationFragment?
What context should I pass to the AdapterItem from InformationFragment?

Time:07-24

AdapterItem

class AdapterItem(val context: Context, val userList: List): RecyclerView.Adapter<AdapterItem.ViewHolder>() {

class ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){

var numeLista: TextView var caloriiLista: TextView

    init{
        numeLista=itemView.numeLista
        caloriiLista=itemView.caloriiLista
    }

}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
   var itemView = LayoutInflater.from(context).inflate(R.layout.row_items, parent, false)
    return ViewHolder(itemView)

}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder.numeLista.text=userList[position].foodItems.get(0).foodName
    holder.caloriiLista.text=userList[position].foodItems.get(0).calories.toString()

}

override fun getItemCount(): Int {
    return userList.size
}

}

InformationFragment:

const val BASE_URL= "https://raw.githubusercontent.com/terrenjpeterson/caloriecounter/master/src/data/"

class InformationFragment : Fragment() {

private var binding: FragmentInformationBinding?   =null
private  val sharedViewModel: SharedViewModel by activityViewModels()

lateinit var adapterItem: AdapterItem
lateinit var linearLayoutManager: LinearLayoutManager



override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    recyclerview_lista.setHasFixedSize(true)
    linearLayoutManager = LinearLayoutManager(this.context)
   recyclerview_lista.layoutManager=linearLayoutManager

    getMyData()




    val fragmentBinding = FragmentInformationBinding.inflate(inflater, container, false)
    binding= fragmentBinding
    return fragmentBinding.root
}



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

    binding?.apply {
        viewModel= sharedViewModel
        informationFragment=this@InformationFragment
        lifecycleOwner= viewLifecycleOwner
    }

}
    private fun getMyData() {
    val retrofitBuilder= Retrofit.Builder()
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl(BASE_URL)
        .build()
        .create(ApiInterface::class.java)

    val retrofitData= retrofitBuilder.getData()

    retrofitData.enqueue(object : Callback<List<MyDataItem>?> {
        override fun onResponse(
            call: Call<List<MyDataItem>?>,
            response: Response<List<MyDataItem>?>
        ) {
            val responseBody= response.body()!!
            adapterItem= AdapterItem(,responseBody ) // ** here what context should I pass ???? ... I cant use baseContext**
            adapterItem.notifyDataSetChanged() 
            recyclerview_list.adapter=adapterItem



        }


        override fun onFailure(call: Call<List<MyDataItem>?>, t: Throwable) {
           Log.d( "informationFragment" , "onFailure: "   t.message)
        }
    })
}

}

CodePudding user response:

There is no need to pass the context as an arg you can use the context of the parent passed to your viewHolder

you shouldn't pass context. change:

class AdapterItem(val context: Context, val userList: List):

to:

class AdapterItem(val userList: List):

and

var itemView = LayoutInflater.from(context).inflate(R.layout.row_items, parent, false)

to:

var itemView = LayoutInflater.from(parent.context).inflate(R.layout.row_items, parent, false)

Be careful if you want to pass context to the adapter, if you assigned the view context to a variable in your adapter, it probably would lead to a memory leak. Consider using WeakReference to avoid that if happens.

CodePudding user response:

You can use the context of view parameter of your viewholder.

CodePudding user response:

My app still keep crashing when it reaches the InformationFragment

class AdapterItem( val userList: List): RecyclerView.Adapter<AdapterItem.ViewHolder>() {

class ViewHolder(itemView: View):RecyclerView.ViewHolder(itemView){
    var numeLista: TextView
    var caloriiLista: TextView
    init{
        numeLista=itemView.numeLista
        caloriiLista=itemView.caloriiLista
    }

}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    var itemView = LayoutInflater.from(parent.context).inflate(R.layout.row_items, parent, false)

    return ViewHolder(itemView)

}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder.numeLista.text=userList[position].foodItems.get(0).foodName
    holder.caloriiLista.text=userList[position].foodItems.get(0).calories.toString()

}

override fun getItemCount(): Int {
    return userList.size
}

}

const val BASE_URL= "https://raw.githubusercontent.com/terrenjpeterson/caloriecounter/master/src/data/"

class InformationFragment : Fragment() {

private var binding: FragmentInformationBinding? = null
private val sharedViewModel: SharedViewModel by activityViewModels()

lateinit var adapterItem: AdapterItem
lateinit var linearLayoutManager: LinearLayoutManager


override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    recyclerview_lista.setHasFixedSize(true)
    linearLayoutManager = LinearLayoutManager(this.context)
    recyclerview_lista.layoutManager = linearLayoutManager

    getMyData()


    val fragmentBinding = FragmentInformationBinding.inflate(inflater, container, false)
    binding = fragmentBinding
    return fragmentBinding.root
}


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

    binding?.apply {
        viewModel = sharedViewModel
        informationFragment = this@InformationFragment
        lifecycleOwner = viewLifecycleOwner
    }

}

private fun getMyData() {
    val retrofitBuilder = Retrofit.Builder()
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl(BASE_URL)
        .build()
        .create(ApiInterface::class.java)

    val retrofitData = retrofitBuilder.getData()

    retrofitData.enqueue(object : Callback<List<MyDataItem>?> {
        override fun onResponse(
            call: Call<List<MyDataItem>?>,
            response: Response<List<MyDataItem>?>
        ) {
            val responseBody = response.body()!!
            adapterItem = AdapterItem(responseBody)
            adapterItem.notifyDataSetChanged()
            recyclerview_lista.adapter=adapterItem
        }


        override fun onFailure(call: Call<List<MyDataItem>?>, t: Throwable) {
             d("informationFragment", "onFailure: "   t.message)
        }
    })
}

}

  • Related