简体   繁体   中英

how to access method in activity from the adapter class using kotlin

I have some text field and a recyclerview in my activity. I need to access textfield values from the recycler view adapter class and access a method in activity from the adapter class. but I couldn't do the same using Kotlin help me to solve this

Activity

   override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_addstaff)
    try {
        sprefs = this!!.getSharedPreferences(
                Config.PACKAGE_NAME, Context.MODE_PRIVATE)
        token = sprefs!!.getString("token", "No")!!
    } catch (e: Exception) {

    }
    try {


        layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        outletnameslistrecycle!!.layoutManager = layoutManager
        Outnameslist = ArrayList()
        adapter = outletname_adapter(Outnameslist as ArrayList<Outletnamelist>, this,btneditstaff as Button)
        outletnameslistrecycle!!.adapter = adapter
        getoutletlist();
    } catch (e: Exception) {

        e.printStackTrace()
    }

}

Adapterclass

class outletname_adapter(internal var Outnamelist: List<Outletnamelist>, private val context: Context, val button: Button) : RecyclerView.Adapter<outletname_adapter.ViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
     try {
    val v = LayoutInflater.from(parent.context)
            .inflate(R.layout.lay_outletnamelist, parent, false)


    return ViewHolder(v, context, Outnamelist)

   } catch (e: Exception) {

        e.printStackTrace()
    }

    return null!!
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    try {

     button.setOnClickListener {


     }

        var OutletList =Outnamelist[position]
        holder.outlet_name!!.text=OutletList.name

    } catch (e: Exception) {
        e.printStackTrace()
    }

}

override fun getItemCount(): Int {

    return Outnamelist.size

}

inner class ViewHolder(v: View, var context: Context, private val outnamelist: List<Outletnamelist>) 
  : RecyclerView.ViewHolder(v), View.OnClickListener {

     var outlet_checkbox: CheckBox? = null
      var outlet_name: TextView?=null


    var layTop: LinearLayout?= null
    init {
        itemView.setOnClickListener(this)
        try {
           outlet_checkbox=v.findViewById<View>(R.id.checkBox) as CheckBox
            outlet_name=v.findViewById<View>(R.id.txt_in)as TextView


        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onClick(v: View) {
        val position = adapterPosition
    }
}

}

XML `

<ScrollView xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/diagonal_bg"
    tools:context=".Activity_addstaff">

    <de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/img_staff"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginStart="30dp"
        android:layout_marginTop="137dp"
        android:src="@drawable/subadmin"
        app:civ_border_color="#ffffff"
        app:civ_border_width="2dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingLeft="1dp"
        android:paddingRight="1dp"
        android:layout_marginTop="250dp">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:animateLayoutChanges="true">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:animateLayoutChanges="true">

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/staffname_text_input"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/staffname_edit_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:hint="@string/label_staffname" />
                </android.support.design.widget.TextInputLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:animateLayoutChanges="true">

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/staffaddress_text_input"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/staffaddress_edit_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:hint="@string/label_address" />
                </android.support.design.widget.TextInputLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:animateLayoutChanges="true">

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/staffplace_text_input"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/staffplace_edit_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:hint="@string/label_place" />
                </android.support.design.widget.TextInputLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:animateLayoutChanges="true">

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/staffpin_text_input"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/staffpin_edit_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:hint="@string/label_pin" />
                </android.support.design.widget.TextInputLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:animateLayoutChanges="true">

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/staffmob_text_input"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/staffmob_edit_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:hint="@string/label_mob"
                        />
                </android.support.design.widget.TextInputLayout>
            </LinearLayout>

            <LinearLayout

                android:id="@+id/lay_root"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="5dp">


                <LinearLayout

                    android:id="@+id/lay_root2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/outletnameslistrecycle"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="1dp" />

                </LinearLayout>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp"
                android:orientation="horizontal"
                android:padding="20dp">

                <Button
                    android:id="@+id/btneditstaff"
                    android:layout_width="wrap_content"
                    android:layout_height="35dp"
                    android:layout_gravity="center"
                    android:layout_marginEnd="5dp"
                    style="@style/DefaultButton"
                    android:background="@drawable/btn_border"
                    android:foreground="?attr/selectableItemBackground"

                    android:text="Save"
                    android:textAllCaps="false"
                    android:textColor="@color/white" />

            </LinearLayout>



        </LinearLayout>


    </LinearLayout>





</RelativeLayout>

From the activity, I have passed my button reference to adapter class but the same way I am getting an error when trying to pass textfield references.

you can access the activity method or others on this way in adapter:

if(context is XXXActivity){
   context.xxx()
   context.xx
}

but this isn't the good code.you can try like this 2 methods:

  1. you can define and set a Listener to adapter to post the event(such as click?) to activity, make the activity deal with it initiative(activity can get the data from itself and adapter)。
  2. if not an event make the adapter need the data from activity.you can provide the data from activity with interface like this.
class outletname_adapter(internal var Outnamelist: List<Outletnamelist>, private val context: Context, val button: Button ,   val xxxtextProvider:()->String={""}/*it return the data you need*/ ) : RecyclerView.Adapter<outletname_adapter.ViewHolder>() {
...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM