This is my code for the MainFragment file
package hr.ferit.tomislav.lucic5.tl5_projekt
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 androidx.recyclerview.widget.RecyclerView
class MainFragment : Fragment() {
private lateinit var transactions : ArrayList<Transaction>
private lateinit var transactionAdapter: TransactionAdapter
private lateinit var linearlayoutManager: LinearLayoutManager
private var recyclerview : RecyclerView? = view?.findViewById(R.id.recyclerview)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_main, container, false)
transactions = arrayListOf(
Transaction("President", 400.00),
Transaction("Jolly", 300.00)
)
linearlayoutManager = LinearLayoutManager(context)
transactionAdapter = TransactionAdapter(transactions)
recyclerview?.apply {
layoutManager = linearlayoutManager
adapter = transactionAdapter
}
return view
}
}
This is the TransactionAdapter file
package hr.ferit.tomislav.lucic5.tl5_projekt
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
class TransactionAdapter(val transactions: ArrayList<Transaction>) :
RecyclerView.Adapter<TransactionAdapter.TransactionHolder>() {
class TransactionHolder(val view: View) : RecyclerView.ViewHolder(view) {
val label : TextView = view.findViewById(R.id.label)
val amount : TextView = view.findViewById(R.id.amount)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TransactionHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.transaction_layout, parent, false)
return TransactionHolder(view)
}
override fun onBindViewHolder(holder: TransactionHolder, position: Int) {
val transaction = transactions[position]
val context = holder.amount.context
if(transaction.amount >= 0){
holder.amount.text = " $%.2f".format(transaction.amount)
holder.amount.setTextColor(ContextCompat.getColor(context, R.color.green))
}else {
holder.amount.text = "- $%.2f".format(Math.abs(transaction.amount))
holder.amount.setTextColor(ContextCompat.getColor(context, R.color.red))
}
holder.label.text = transaction.label
}
override fun getItemCount(): Int {
return transactions.size
}
}
After debuging the transactions don't show, it's a college project and we have to use only one activity and then fragments, when i'm using a similar code in activities the transactions show but not in fragments
I've tried making some changes with initializing the adapter but nothing worked
CodePudding user response:
I have solved it by deleting everything from onCreateView, and overriding onViewCreated:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
transactions = arrayListOf(
Transaction("President", 400.00),
Transaction("Jolly", 300.00)
)
var layoutManager = LinearLayoutManager(context)
recyclerView = view.findViewById(R.id.recyclerview)
recyclerView.layoutManager = layoutManager
adapter = TransactionAdapter(transactions)
recyclerView.adapter = adapter
}