Kotiln:将数据从适配器传递到活动
我尝试使用putExtra
将数据从adapter
传递到其他activity
,但是当我单击列表中的项目以移动到第二个activity
时,没有数据被检索,并且我输入的默认文本不会显示.
另一种方法呢?或我想念什么?
这是我的代码:
I try to pass my data from the adapter
to my other activity
with a putExtra
but when I click on an item in my list to move to my second activity
, no data is retrieved and the default text I put in is not displayed.
another way to do? or What do I miss about it?
Here my code :
我的
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)
}
}
我的第二个
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}")
}
}
方法1:
您可以使用callback
首先,在适配器中定义一个回调,如下所示:
You can use callback
First of all, define a callback in your adapter like this :
interface CallbackInterface {
fun passDataCallback(message: String)
}
然后在adapter
中初始化回调interface
:
class YourAdapter(private val callbackInterface:CallbackInterface) :
RecyclerView.Adapter<CurrencyListAdapter.ViewHolder>() {
.
.
.
}
然后从onBindViewHolder()
内部的接口使用回调方法,如下所示:
Then use the callback method from the interface inside your onBindViewHolder()
like this :
holder.itemView.setOnClickListener {
//Set your codes about intent here
callbackInterface.passResultCallback("Your message")
}
最后,像这样在activity
中实现callback
方法:
And finally, implement your callback
method in your activity
like this :
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"
}
}
更新:
方法2:
如果您不使用callback
,则只需将activity
更改为:
If you dont use callback
, as you wrote just change your activity
to this :
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 BASE
我们可以将乐趣传递给适配器,并像这样从适配器获取数据:
We can pass a fun to adapter and get data from it like this :
在我们的适配器中:
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) }
}
}
在我们的片段或活动中,我们可以通过以下方式获取数据:
In our fragment or activity we can get data with this way :
YourAdapter { yourData ->
// we can use yourData here
}