如何使用kotlin从适配器类访问活动中的方法

问题描述 投票:-1回答:1

我的活动中有一些文本字段和一个recyclerview。我需要从回收者视图适配器类访问文本字段值,并从适配器类访问活动中的方法。但是我不能使用Kotlin来做同样的事情来帮助我解决这个问题

活动

   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>

从活动中,我已经将按钮引用传递给了适配器类,但是与尝试传递文本字段引用时遇到错误的方式相同。

android kotlin recycler-adapter
1个回答
0
投票

您可以通过这种方式在适配器中访问活动方法或其他方法:

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

但是这不是很好的代码。您可以尝试以下两种方法:

  1. 您可以定义并将侦听器设置为适配器,以将事件(例如click?)发布到活动中,使活动主动处理(活动可以从其自身和适配器获取数据)。
  2. 如果不是事件,则使适配器需要来自活动的数据。您可以通过这样的接口提供来自活动的数据。
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>() {
...
}
© www.soinside.com 2019 - 2024. All rights reserved.