简体   繁体   English

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

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

I have some text field and a recyclerview in my activity.我的活动中有一些文本字段和一个 recyclerview。 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但我不能用 Kotlin 帮我解决这个问题

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 ` 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)。你可以定义并设置一个Listener到adapter来发布事件(比如click?)到activity,让activity主动处理它(activity可以从自身和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>() {
...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Kotlin从RecyclerView适配器类完成活动 - How to finish an activity from RecyclerView adapter class using Kotlin 使用带有 Kotlin 的接口回调方法将数据从适配器传递到活动 - Pass data from adapter to activity using interface callback method with Kotlin 使用 Kotlin 从 RecyclerView Adapter 调用 Activity 方法 - Call Activity method from RecyclerView Adapter using Kotlin 如何使用 kotlin 从适配器中的活动中获取价值 - How to get value from activity in adapter using kotlin 如何在 Kotlin 中使用 `getString()` 从 Recycler Adapter 类访问 `strings.xml` 中的字符串 - How can I access strings in `strings.xml` from a Recycler Adapter class using `getString()` in Kotlin Java Class 从 Kotlin 中的适配器访问数据 - Java Class to access data from Adapter in Kotlin 如何从Kotlin Android中的适配器启动活动 - How to start activity from adapter in kotlin android 如何从RecyclerView适配器中的活动访问变量和方法 - How to access variables and method from an Activity inside a RecyclerView Adapter 在从 Activity 调用的类中创建适配器时,如何访问 Activity 中的适配器? - How to access to an adapter in an Activity when the adapter is created in a class which is called from the Activity? Kotlin 访问 recyclerView 适配器上的活动 - Kotlin access activity on recyclerView Adapter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM