gpt4 book ai didi

android - Kotlin : pass data from adapter to activity

转载 作者:搜寻专家 更新时间:2023-11-01 09:20:08 27 4
gpt4 key购买 nike

我尝试使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com