Android – Kotlin: data through fragment 1 to 2 and 2 to one

I am currently developing an Android application thanks to Kotlin.

I am using the default navigation drawer and the fragment. I want to do something like this:

  1. In the current fragment, put some data in a spinner (done)
  2. When clicking on a text view, keep the data (of the selected selector) (almost finished), then (3)
  3. Go to another fragment (fact)
  4. In this fragment, create a spinner with some data and send it to the first fragment (almost finished)
  5. Insert the new value And the old the first fragment

Then I have two spinners, and the second (in the second fragment) has an adapter.

My question is: is there a way to do it more easily? I'm struggling with all those packages between the fragments and the adapter, but I really think there's a simpler way of what I'm doing …

First fragment:

undo the fun inCreateView (inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle?): View? {
val v = inflater.inflate (R.layout.mes_informations, container, false)
val thisBundle = this.arguments
if (thisBundle! = null) {
val builder = StringBuilder ("Extras:  n")
for (key in thisBundle.keySet ()) {
value val = thisBundle.get (key)
builder.append (key) .append (":") .append (value) .append (" n")
}
selectedArret.text = thisBundle.get ("Arret"). toString ()
}

go back
}

cancel the onViewCreated fun (see: View, savedInstanceState: Bundle?) {
super.onViewCreated (see, savedInstanceState)

val thisBundle = this.arguments

if (thisBundle! = null) {
val builder = StringBuilder ("Extras:  n")
for (key in thisBundle.keySet ()) {
value val = thisBundle.get (key)
builder.append (key) .append (":") .append (value) .append (" n")
}
Log.i (TAG, builder.toString ())
}

package = package ()


spinnerDepartement.onItemSelectedListener = object: AdapterView.OnItemSelectedListener {
cancel fun onItemSelected (parent: AdapterView<*>, view: view, position: int, id: long) {
when (position) {
position -> departement = bundle.putString ("departement", spinnerDepartement.selectedItem.toString ())
else -> bundle.putString ("departement", "Cher")
}
Log.i (TAG, spinnerDepartement.selectedItem.toString ())
}

cancel the fun inNothingSelected (father: AdapterView<*>) {
}
}


val fragmentTransaction = fragmentManager? .beginTransaction ()
val rechercheFragm = RechercherArret ()
rechercheFragm.arguments = bundle
ligneReguliereLayout.setOnClickListener {
fragmentTransaction
? .replace (R.id.content_frame, rechercheFragm)
? .addToBackStack (null)
?.commit()
}
}

The second :

            cancel the onViewCreated fun (see: View, savedInstanceState: Bundle?) {
super.onViewCreated (see, savedInstanceState)
lignes = ArrayList ()

val resultArgument = arguments

val queryFill = resources.getStringArray (R.array.fillSearchQuery2)
for (ligne in queryFill) {
lignes.add (ligne)
}

adapter = ListAdapterCustom (view.context, R.layout.list_adapter, lignes, resultArgument)

listSearchView.adapter = adapter

search.queryHint = "Entrez un arrĂȘt"

search.setOnQueryTextListener (object: SearchView.OnQueryTextListener {

annular fun onQueryTextChange (newText: String): Boolean {
adapter.filter.filter (newText.trim ())
if (newText.trim ()! = "") {
listSearchView.visibility = View.VISIBLE
}plus{
listSearchView.visibility = View.GONE
}
false return
}

override fun onQueryTextSubmit (query: String): Boolean {
Toast.makeText (view.context, "Submit $ query", Toast.LENGTH_SHORT) .show ()
false return
}

})
}

And the adapter:

class ListAdapterCustom (context: context, resource: int, list: ArrayList, val private arguments: Bundle?): ArrayAdapter(context, resource, list) {


cancel fun getView (position: Int, convertView: View, parent: ViewGroup): View {
val view = super.getView (position, convertView, parent)

val thisBundle = arguments
if (thisBundle! = null) {
val builder = StringBuilder ("Extras:  n")
for (key in thisBundle.keySet ()) {
value val = thisBundle.get (key)
builder.append (key) .append (":") .append (value) .append (" n")
}
Log.i ("Extras", builder.toString ())
}

val arret = view.findViewById(R.id.arret)

arret.setOnClickListener {
fragment of val = MesInformations ()
package val = package ()
bundle.putString ("Arret", arret.text.toString ())
fragment.arguments = package

Snackbar.make (see, arret.text, Snackbar.LENGTH_SHORT) .show ()

val fragmentManager = (context as AppCompatActivity) .supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction ()

fragmentTransaction.replace (R.id.content_frame, fragment) .addToBackStack (null)
fragmentTransaction.commit ()
fragmentTransaction.addToBackStack (null)
}

yes (position% 2 == 1) {
view.setBackgroundResource (R.color.colorWhite)
} else {
view.setBackgroundResource (R.color.grayBackground)
}
return view
}

}