1

I'm new in Kotlin and I making an app which is currency changer. And in the adapter, I want to pass some items into a new activity.

 class AdapterC (val countryList: ArrayList<CountriesData>): RecyclerView.Adapter<AdapterC.ViewHolder>() {

    override fun onCreateViewHolder(view: ViewGroup, position: Int): ViewHolder {

         val v = LayoutInflater.from(view?.context).inflate(R.layout.country_list,view,false)
         return  ViewHolder(v)
    }

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

    override fun onBindViewHolder(view: ViewHolder, position: Int) {

          val country : CountriesData=countryList.toTypedArray()[position]
          view?.textViewName.text=country.name

            /*
             Old code in which i can accese to items        
             view.itemView.setOnClickListener{
             var name = country.name
             var id = country.id
             }
            */

     }


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

           val textViewName= itemView.findViewById(R.id.TextViewCountry) as TextView

         //New code that i found on the internet

           init {

                  itemView.setOnClickListener{

                      val intent = Intent(itemView.context, CurrencyActivity::class.java)
                      itemView.context.startActivity(intent)

                   }
            }
      }

  }

As I know that is a bad practice to put setOnClickListener inside onBindViewHolder and I couldn't start a new activity in there so I looked on the Internet and there was the solution to start new Activity inside ViewHolder class. But now I don't know how to pass an item, that was clicked into the new activity.

Below is data class

data class CountriesData(val name :String,val id :String)
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
TiPSY
  • 107
  • 2
  • 12

3 Answers3

1

RecyclerView Adapter in Kotlin

Used Anko https://github.com/Kotlin/anko

Anko is a Kotlin library which makes Android application development faster and easier. It makes your code clean and easy to read, and lets you forget about rough edges of the Android SDK for Java.

class StackAdapter(val context: Context, val countryList: ArrayList<CountriesData>) : RecyclerView.Adapter<StackAdapter.StackViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StackViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false)
        return StackViewHolder(view)
    }

    override fun getItemCount(): Int = countryList.size

    override fun onBindViewHolder(holder: StackViewHolder, position: Int) {
            holder.setUpViewHolder(countryList[position])
    }

    inner class StackViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val Name: TextView = itemView.txtView

        fun setUpViewHolder(countries: CountriesData){
            Name.text = countries.name
            Name.setOnClickListener {
                context.startActivity<CurrencyActivity>(COUNTRIES to countries)
            }
        }
    }
}

How to fetch data in Activity

    data = intent.getStringExtra(COUNTRIES)

Note COUNTRIES is just a key in Constants.kt

This how you do that in Kotlin. const val HEADER_USER_TYPE = "userType"

Sharan
  • 1,055
  • 3
  • 21
  • 38
0

You can send the CountriesData like this

This is how I do in Java

 @Override
 public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int index) 
  {

    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            CountriesData countryData = countryList.get(index);
            Intent intent= new Intent(context, NewActivity.class);
            intent.putExtra("key", countryData );
            context.startActivity(intent);
        }
 }

Note

If you are passing Class object then your class object should implement either Serializable or Parceableinterface.

This is how your CountryData class should look like

public class CountryData implements Serializable{
  //  getter and setter

}

How to retrieve value?

You can get the Class object using key value. Don't forget to Typecast.

Intent i = getIntent();
CountryData countrydata = (CountryData)i.getSerializableExtra("key");
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
-1

You should use getAdapterPosition() inside your ViewHolder class to get the position of clicked item by the user

OR

You can also put your onClick code to onCreateViewHolder() and then use

vh = ViewHolder(v)
vh.textViewName.setOnClickListener {
//Your code to start activity
val intent = Intent(itemView.context, CurrencyActivity::class.java)
val bundle = Bundle()
bundle.putString("name", name)
bundle.putString("id", id)
intent.putExtras(bundle)
itemView.context.startActivity(intent)
}
Mayur Prajapati
  • 597
  • 1
  • 6
  • 14