Home > other >  How to send my room database saving image or data one activity to another
How to send my room database saving image or data one activity to another

Time:08-18

Hello everyone I tring to do when I click my RecyclerView Item then it go another activity and show RecyclerView Clicked item image and title. MY recyclerView Item click to go another activity work done but how I can solve image and title send another activity. I am using room database. So anyone please help me.

MainActivity

  private fun getAllCompletedData(registerDao: registerDao){
        lifecycleScope.launch {
            registerDao.fetchAllData().collect { allCompletedData->
                val list = ArrayList(allCompletedData)
                setUpListOfDataIntoRecyclerView(list, registerDao)

            }
        }
    }

 private fun setUpListOfDataIntoRecyclerView(registerList:ArrayList<registerEntity>,
                                                registerDao: registerDao){


        if(registerList.isNotEmpty()){
            val itemAdapter = MainAdapter(registerList)

            itemAdapter.onItemClick = { goToAnotherActivity()}

            binding?.rvHappyPlaceList?.layoutManager = LinearLayoutManager(this)
            binding?.rvHappyPlaceList?.adapter = itemAdapter
            }
    }


    fun goToAnotherActivity() {

        val intent = Intent(this, HappyPlaceDetailsActivity::class.java)
        startActivity(intent)
    }

MainAdapter

class MainAdapter(val items: ArrayList<registerEntity>,
                  var onItemClick: (() -> Unit)? = null):
    RecyclerView.Adapter<MainAdapter.MainHolder>() {

    private var onClickListener: AdapterView.OnItemClickListener? = null

    inner class MainHolder(var binding: ItemHappyPlaceBinding): RecyclerView.ViewHolder(binding.root) {


        init {
            binding.root.setOnClickListener {
                onItemClick?.invoke()
            }
        }
                                val llTitle = binding.tvTitle
                                 val llDescription = binding.tvDescription
                                  var llImage = binding.ivPlaceImage
                         }

When I click any RecyclerView Item mostly I am trying to send Image recyclerView to HappyPlaceDetailsActiviy . IN this activiy.xml I have Imageview and title.

CodePudding user response:

Try this

Data class, can be modify to your

import java.io.Serializable

data class RegisterEntity(
   var llImage: String,
   var llTitle: String,
   var llDescription: String
): Serializable

Adapter class

class MainAdapter(val items: ArrayList<RegisterEntity>,
              var onItemClick: ((RegisterEntity) -> Unit)? = null):
RecyclerView.Adapter<MainAdapter.MainHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainAdapter.MainHolder {
    val layoutInflater = LayoutInflater.from(parent.context)
    val binding: ListItemBinding = DataBindingUtil.inflate(
        layoutInflater,
        R.layout.list_item,
        parent,
        false
    )
    return ItemHappyPlaceBinding(binding)
}

override fun onBindViewHolder(holder: MainAdapter.MainHolder, position: Int) {
    val item= items[position]
    holder.bind(item)
}

override fun getItemCount(): Int  = items.size

inner class MainHolder(val binding: ItemHappyPlaceBinding) :
    RecyclerView.ViewHolder(binding.root) {

        fun bind(item: RegisterEntity) {
            val llTitle = binding.tvTitle
            val llDescription = binding.tvDescription
            var llImage = binding.ivPlaceImage
            // you should be able to set the text here
            //e.g llTitle.text = item.title

            binding.root.setOnClickListener {
                onItemClick?.invoke(item)
            }
            llTitle.text = item.llTitle.toString()
            llDescription.text = item.llDescription.toString()
        
        }
     }
  }

Now in your MainActivity

class FirstActivity : AppCompatActivity() {
var binding: ActivityMainBinding? = null
val items: ArrayList<RegisterEntity> = arrayListOf()

lateinit var adapter: MainAdapter
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
    adapter = MainAdapter(items){
        goToAnotherActivity(it)
    }


    val registerDao: registerDao = registerDao()// replace with your dao
    getAllCompletedData(registerDao)

}

private fun getAllCompletedData(registerDao: registerDao){
    lifecycleScope.launch {
        registerDao.fetchAllData().collect { allCompletedData->
            val list = ArrayList(allCompletedData)
            setUpListOfDataIntoRecyclerView(list, registerDao)

        }
    }
}

private fun setUpListOfDataIntoRecyclerView(registerList:ArrayList<RegisterEntity>,
                                            registerDao: registerDao){


    if(registerList.isNotEmpty()){
        val itemAdapter = MainAdapter(registerList)

        itemAdapter.onItemClick = { goToAnotherActivity()}

        binding?.rvHappyPlaceList?.layoutManager = LinearLayoutManager(this)
        binding?.rvHappyPlaceList?.adapter = itemAdapter
    }
}


private fun goToAnotherActivity(registerEntity: RegisterEntity) {
    val intent = Intent(this, HappyPlaceDetailsActivity::class.java)
    intent.putExtra("data", registerEntity)
    startActivity(intent)
  }

}

Now in your second activity that want to receive the object

 class HappyPlaceDetailsActivity : AppCompatActivity() {
var binding: ActivityMainBinding? = null
val items: ArrayList<RegisterEntity> = arrayListOf()

lateinit var adapter: MainAdapter
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = DataBindingUtil.setContentView(this, R.layout.second_activity)


    val registerEntity = intent.getSerializableExtra("data") as? RegisterEntity

    }

   // you should be able to get your way from here

 }

If you need more clarification, check this How to pass values from RecycleAdapter

  • Related