here is my scenario I have 2 fragment A, A consists of recyclerView Adapter to display list of items, and fragment B has 4 edit texts, and an add button, when the user clicks on add button it will navigate back to fragment A, here I'm confused how do i bind the Args from fragment B and add them to the recyclerView as a new item.
here is my code
class ContactListFragment : Fragment() {
private var _binding: FragmentContactListBinding? = null
private val binding get() = _binding!!
private var adapter: ListAdapter? = null
private var listOfStudents: MutableList<StudentInfo>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentContactListBinding.inflate(layoutInflater)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
adapter = ListAdapter(mutableListOf(),requireContext())
binding.rvListContact.adapter = adapter
binding.fabAddItem.setOnClickListener {
val action =
ContactListFragmentDirections
.actionContactListFragmentToAddItemFragment()
it.findNavController().navigate(action)
}
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
AddItemFragment.kt
<pre>class AddItemFragment : Fragment() {
private var _binding: FragmentAddItemBinding? = null
private val binding get() = _binding!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentAddItemBinding.inflate(layoutInflater)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.apply {
btnAddNewContact.setOnClickListener {
if (edAddName.text!!.isEmpty()) {
edAddName.error = "name is required!"
toastMessage("missing required fields!")
return@setOnClickListener
}
if (edAddPhoneNumber.text!!.isEmpty()) {
edAddPhoneNumber.error = "phone number is required"
toastMessage("missing required fields!")
return@setOnClickListener
}
if (edCountry.text!!.isEmpty()) {
edCountry.error = "country is required"
toastMessage("missing required fields!")
return@setOnClickListener
}
val studentName = binding.edAddName.text.toString()
val studentNumber = binding.edAddPhoneNumber.text.toString()
val studentCountry = binding.edCountry.text.toString()
val student = StudentInfo(studentName,studentNumber,studentCountry)
findNavController().navigate(
AddItemFragmentDirections.actionAddItemFragmentToContactListFragment(
studentName,
studentNumber,
studentCountry,
)
)
}
val alertDialog = AlertDialog.Builder(requireContext())
.setTitle("CANCEL DIALOG")
.setMessage("Do you want to cancel adding new card o your list?")
.setPositiveButton("YES"){_,_ ->
findNavController().navigate(
AddItemFragmentDirections.actionAddItemFragmentToContactListFragment())
toastMessage("adding new contact is cancelled!")
}
.setNegativeButton("DISMISS"){dialogInterface,_ ->
dialogInterface.dismiss()
}.create()
btnCancel.setOnClickListener {
if (edAddPhoneNumber.text!!.isEmpty() && (edAddName.text!!.isEmpty())){
findNavController().navigate(
AddItemFragmentDirections.actionAddItemFragmentToContactListFragment()
)
toastMessage("adding new contact is cancelled!")
}
if(edAddPhoneNumber.text!!.isNotEmpty() && (edAddName.text!!.isNotEmpty())){
alertDialog.show()
}
}
}
}
fun toastMessage(text: String) {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
}
}
RecylcerViewAdaptyer.kt
class ListAdapter(private var studentInfo: MutableList<StudentInfo>, private val context: Context): RecyclerView.Adapter<ListAdapter.ItemViewHolder>(){
inner class ItemViewHolder(val binding: ItemViewBinding): RecyclerView.ViewHolder(binding.root)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
val binding = ItemViewBinding.inflate(LayoutInflater.from(parent.context), parent,false)
return ItemViewHolder(binding)
}
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
val currItem = studentInfo[position]
holder.binding.apply {
tvName.text = currItem.name
tvNumber.text = currItem.number
tvCountry.text = currItem.country
}
}
override fun getItemCount(): Int {
return studentInfo.size
}
}
What I have tried:
I created new function to add the item to the list in the Adapter, and notify it when new item is inserted, and i called the function in Fragment A and passed in the args.
<pre>
fun addStudent(studentCard: StudentInfo){
studentInfo.add(studentCard)
notifyItemInserted(studentInfo.size - 1)
}