7

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 :

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}")
    }
}
RKRK
  • 1,284
  • 5
  • 14
  • 18
tibdev78
  • 109
  • 1
  • 2
  • 8
  • perhaps you're not looking well. try Log.e("dd", "${msg}") and switch your Logcat from Verbose/Debug to Error and try again. – Wale Jun 30 '19 at 22:32

1 Answers1

9

Method 1 :

You can use callback First of all, define a callback in your adapter like this :

    interface CallbackInterface {   
        fun passResultCallback(message: String)
    }

Then initialize the callback interface in your adapter :

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

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")
}

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"
    }
}

UPDATE:

Method 2 :

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}")
    }
}

UPDATE : December 26, 2019

Method 3 : KOTLIN BASE

We can pass a fun to adapter and get data from it like this :

In our adapter :

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
            }
milad salimi
  • 1,580
  • 2
  • 12
  • 31
  • Don't you think the solution is supposed to follow his pattern ? I mean it's just to parse data from one Intent to another !!! – Wale Jun 30 '19 at 21:16
  • Ok i see, but how you jump to my activty? , because before, i was doing val intent = Intent(holder.itemView.context, TracksActivity::class.java), but now how i do? – tibdev78 Jun 30 '19 at 21:25
  • @Whales , Please read question again he/she said that "another way to do or What do I miss about it??" then i decide to explain another way – milad salimi Jul 01 '19 at 04:44
  • @tibdev78 , you can set your codes about intent in setOnClickListener in onBindViewHolder – milad salimi Jul 01 '19 at 04:47
  • I see, i test this tonight and keeps you informed – tibdev78 Jul 01 '19 at 09:30
  • @tibdev78 , OK , test both of them with accuracy – milad salimi Jul 01 '19 at 09:35
  • In the first method the function you write is called passDataCallback but then in onbindviewholder you call .passResultCallback. Where is passResultCallback defined as it doesnt resolve for me, yet calling passDataCallback does. I am not at the testing point yet. – UnknownError Jan 11 '21 at 11:46
  • @UnknownError , Thanks i fix it , try it with accuracy – milad salimi Jan 11 '21 at 14:26
  • @miladsalimi thank you. Perhaps you can help me over here https://stackoverflow.com/questions/65666654/pass-data-from-adapter-to-activity-using-interface-callback-method-with-kotlin as I am getting stuck with the companion object using your method 1 – UnknownError Jan 11 '21 at 15:20