Reputation: 23
I'm creating a chat app in android using kotlin and wanted to create a group item based on registered users.
Currently I'm facing a problem on finding a way to pass the variable from the adapter going back to activities because i want to have a floating check button and display what are the selected users from that adapter.
I browse the web but haven't found an ideal write-up about adaptes to activites.
Please point me to some solutions/helpful articles about this. Or if this question is duplicated and answered successfully.
Thanks
AddUserGroupAdapter.kt
....
override fun populateViewHolder(viewHolder: AddUsersGroupAdapter.ViewHolder?, user : Users?, position: Int) {
var userId = getRef(position).key
viewHolder!!.bindView(user!!,context)
viewHolder.itemView.setOnClickListener {
if(viewHolder.userSelected!!){
viewHolder.userSelected = false
//viewHolder!!.bindView(user!!,context)
viewHolder.userCheck!!.visibility = View.GONE
}else{
viewHolder.userSelected = true
viewHolder.userCheck!!.visibility = View.VISIBLE
}
//TODO : I want to collect the Id's into list and
// will access to the activity(AddUserGroupActivity)
//Toast.makeText(context,userId,Toast.LENGTH_LONG).show()
}
}
AddUserGroupActivity.kt
...
class AddUsersGroupActivity : AppCompatActivity() {
var mUserDatabase : DatabaseReference? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_users_group)
supportActionBar!!.setTitle("Add Users in Group")
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
var lineaLayoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
mUserDatabase = FirebaseDatabase.getInstance().reference
.child("Users")
rvAddUsersGroup.setHasFixedSize(true)
rvAddUsersGroup.layoutManager = lineaLayoutManager
rvAddUsersGroup.adapter = AddUsersGroupAdapter(mUserDatabase!!, this)
//TODO : This is where I wanted to have a floating action button onClickListener
//that will access the variable from adapter and display to toast.
}
}
Upvotes: 2
Views: 5093
Reputation: 303
you could pass data from adapter to the activity by three ways
(context.applicationContext as? BaseActivity)?.doSomething(variable)
but if you could get data from outside of Adapter by the number of pressed element, use
... Interface
interface OnItemClickListener {
fun onItemClick(index: Int)
}
... Adapter
var mOnItemClickListener: OnItemClickListener? = null
... Holder
v.setOnClickListener {
mOnItemClickListener?.onItemClick(adapterPosition)
}
... Activity
adapter.mOnItemClickListener = object : OnItemClickListener {
override fun onItemClick(index: Int) {
mListener?.showDetails(index)
}
}
Upvotes: 4
Reputation: 10699
Add listerner to you ViewHolder
which will notify your adapter and in adapter forward this notification to activity.
override fun populateViewHolder(viewHolder: AddUsersGroupAdapter.ViewHolder?, user : Users?, position: Int, userSelectionListener: (Users, Boolean) -> Unit) {
var userId = getRef(position).key
viewHolder!!.bindView(user!!,context)
viewHolder.itemView.setOnClickListener {
if(viewHolder.userSelected!!){
viewHolder.userSelected = false
//viewHolder!!.bindView(user!!,context)
viewHolder.userCheck!!.visibility = View.GONE
usersSelectionListener.invoke(user, true)
}else{
viewHolder.userSelected = true
viewHolder.userCheck!!.visibility = View.VISIBLE
usersSelectionListener.invoke(user, false)
}
//TODO : I want to collect the Id's into list and
// will access to the activity(AddUserGroupActivity)
//Toast.makeText(context,userId,Toast.LENGTH_LONG).show()
}
}
Add same parameter to your Adapter constructor, then in activity handle this listener:
rvAddUsersGroup.adapter = AddUsersGroupAdapter(mUserDatabase!!, this) { user: User, isSelected: Boolean ->
//Do want you want with user and selection.
}
Upvotes: 1