android - Kotlin : pass data from adapter to activity

标签 android android-intent android-activity kotlin android-recyclerview

我尝试使用 putExtra 将我的数据从 adapter 传递到我的其他 activity 但是当我单击列表中的项目时转到我的第二个 activity,没有检索到任何数据,也没有显示我输入的默认文本。 另一种方法呢?或者我想念它什么? 这是我的代码:

My onBindViewHolder:

override fun onBindViewHolder(holder: AlbumsListViewHolder, position: Int) {
    val AlbumsData = albumsData!![position]
    holder.albumsName.text = AlbumsData.title

    Glide.with(holder.itemView)
        .load(AlbumsData.cover)
        .transition(DrawableTransitionOptions.withCrossFade())
        .into(holder.coverImage)

    holder.itemView.setOnClickListener {
        val intent = Intent(holder.itemView.context, TracksActivity::class.java)
        //listener?.onClick(AlbumsData)
        intent.putExtra("dd", "ff")
        holder.itemView.context.startActivity(intent)
    }
}

My second Activity:

class TracksActivity: AppCompatActivity(), TracksView {

    private var albumsAdapter: AlbumsAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        setContentView(R.layout.activity_tracks)

        //albumsAdapter = findViewById(R.id.trackslist_recycleview)
        val msg = intent.getStringExtra("dd")
        Log.d("dd", "${msg}")
    }
}

最佳答案

方法一:

您可以使用回调 首先,像这样在你的适配器中定义一个回调:

    interface CallbackInterface {   
        fun passResultCallback(message: String)
    }

然后在您的适配器中初始化回调接口(interface):

class YourAdapter(private val callbackInterface:CallbackInterface) :
    RecyclerView.Adapter<CurrencyListAdapter.ViewHolder>() {
.
.
.
}

然后使用 onBindViewHolder() 中接口(interface)的回调方法,如下所示:

holder.itemView.setOnClickListener {
        //Set your codes about intent here
        callbackInterface.passResultCallback("Your message")
}

最后,在您的 activity 中实现您的 callback 方法,如下所示:

class TracksActivity: AppCompatActivity(), TracksView , YourAdapterName.CallbackInterface {

    private var albumsAdapter: AlbumsAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        setContentView(R.layout.activity_tracks)
    }

  override fun passResultCallback(message: String) {
         //message is "ff"
    }
}

更新:

方法二:

如果你不使用 callback,正如你所写的那样,只需将你的 activity 更改为:

class TracksActivity: AppCompatActivity(), TracksView {

    private var albumsAdapter: AlbumsAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        setContentView(R.layout.activity_tracks)

        var bundle : Bundle? = intent.extras
        var message = bundle!!.getString("dd") 
        Log.d("dd", "${message}")
    }
}

更新:2019 年 12 月 26 日

方法 3:KOTLIN 基础

我们可以像这样将一个 fun 传递给适配器并从中获取数据:

在我们的适配器中:

class YourAdapter(private val clickListener: (yourData: YourData) -> Unit) :
    RecyclerView.Adapter<YourAdapter.ViewHolder>() {

//YourData like String

//And we have onCreateViewHolder like this 

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder = ViewHolder(
        LayoutInflater.from(parent.context).inflate(R.layout.your_item, parent,false),
        clickListener
    )

//And we have ViewHolder class like this 

  inner class ViewHolder(itemView: View, private val clickListener: (yourData: YourData) -> Unit) :
        RecyclerView.ViewHolder(itemView) {
.
.
.
     init {
            initClickListeners()
        }

//And pass data here with invoke


        private fun initClickListeners() {
            itemView.setOnClickListener { clickListener.invoke(yourData) }
        }
}

在我们的 fragment 或 Activity 中,我们可以通过这种方式获取数据:

YourAdapter { yourData ->
            // we can use yourData here
            }

关于android - Kotlin : pass data from adapter to activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56826346/

相关文章:

android - 如何解决警告 : You are installing software that contain unsigned content and authenticity and validity of this software can not established

android - 写 Parcelable 对象得到 java.io.NotSerializableException

android - 使用 Android 使用 Microsoft Office 移动应用程序以编辑模式打开本地文件

java - 显示来自 onCreate 的 Activity

android - android中的自定义 ListView

java - 在应用程序购买中工作,但方法未执行?

android - Phonegap 的 WebIntent 插件 : Cannot find import org. apache.cordova.api.Plugin

android - 获取最新意向

android - 防止 Activity 堆栈被恢复?

android - 如何在不知道当前 Activity 的情况下在 Android 应用程序中显示对话框