Quando já tem um card adicionado e vou adicionar outro logo em seguida, esse adicionado recentemente substitui o card anterior, como faço para manter ambos um em baixo do outro fazendo uma lista do recyclerview? Pesquisei sobre o shared preferences e cheguei implementar ele, mas quando eu fui adicionar outro card contendo outro já existente esse novo card não apareceu.
class BudgetsFragment : Fragment() {
private lateinit var adapter: CardAdapter
private lateinit var recyclerView: RecyclerView
private lateinit var cardList: List<CardItem>
private val retrofit = Retrofit.Builder()
.baseUrl("https://647fc34cf061e6ec4d485612.mockapi.io/")
.addConverterFactory(GsonConverterFactory.create())
.build()
private val apiService: CardService = retrofit.create(CardService::class.java)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView = inflater.inflate(R.layout.fragment_budgets, container, false)
recyclerView = rootView.findViewById(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(context)
val itemId = arguments?.getInt("itemId", -1) ?: -1
if (::cardList.isInitialized && cardList.isNotEmpty()) {
adapter = CardAdapter(cardList.toMutableList())
recyclerView.adapter = adapter
} else {
apiService.getCards().enqueue(object : Callback<List<CardItem>> {
override fun onResponse(call: Call<List<CardItem>>, response: Response<List<CardItem>>) {
if (response.isSuccessful) {
val cards = response.body() ?: emptyList()
val filteredList = cards.filter { it.id == itemId }
if (filteredList.isEmpty()) {
Toast.makeText(context, "Seu orçamento está vazio.", Toast.LENGTH_SHORT).show()
}
cardList = filteredList
adapter = CardAdapter(cardList.toMutableList())
recyclerView.adapter = adapter
}
}
override fun onFailure(call: Call<List<CardItem>>, t: Throwable) {}
})
}
return rootView
}
}
class CardAdapter(
private var cardList: List<CardItem>
) : RecyclerView.Adapter<CardAdapter.CardViewHolder>() {
class CardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val textViewCardTitle: TextView = itemView.findViewById(R.id.textViewCardTitle)
val imageViewCardItem: ImageView = itemView.findViewById(R.id.imageViewCardItem)
val imageButtonDeleteCard: ImageButton = itemView.findViewById(R.id.imageButtonDeleteCard)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_card_test, parent, false)
return CardViewHolder(itemView)
}
override fun onBindViewHolder(holder: CardViewHolder, position: Int) {
val currentItem = cardList[position]
holder.textViewCardTitle.text = currentItem.nome
Picasso.get().load(currentItem.imagem).into(holder.imageViewCardItem)
holder.imageButtonDeleteCard.setOnClickListener {
removeCard(position)
}
}
private fun removeCard(position: Int) {
val updatedList = cardList.toMutableList()
updatedList.removeAt(position)
cardList = updatedList.toList()
notifyItemRemoved(position)
}
override fun getItemCount(): Int {
return cardList.size
}
}
Função do button do DetailsFragment que envia o card:
val buttonDetails = view.findViewById<AppCompatButton>(R.id.buttonDetails)
buttonDetails.setOnClickListener {
val bundle = Bundle()
bundle.putInt("itemId", detailsItem.id)
val navController = findNavController()
navController.navigate(R.id.nav_budgets, bundle)
}