Skip to content Skip to sidebar Skip to footer

How To Pass Data From A Activity To A Recycler View Adapter In Android

I'm trying to design a page where address are stored in recycler view -> cardview. When the user clicks the add address button from the Activity A the user is navigated to the a

Solution 1:

I finally achieved my goal with the use of ActivityResult. Now I'm able to pass data from Activity to Cardview.

Answer : When button is clicked in Activity A, I start the activity with startResultActivity(). Later, when the Activity B i triggered. The end-user inputs the data and that data is passed with the use of putExtra() and once the save button is clicked in Activity B next setResult() in Activity B and finish().

Finally i define onActivityResult() in Activity A to get the result. Works well!!

Solution 2:

I would create a global variable and then store all the data in that variable and simply just call that variable in adapter.

declare a global variable and assign null value to it:

publicstaticStringchecking=null;

a then store data in when you need it:

checking = check.getText().toString();

then call it in your adapter class.

Solution 3:

first make interface listener inside listener make function with parameter like this

interfaceYourRecycleViewClickListener{
    funonItemClickListener(param1:View, param2: String)
}

now extend your activity

classYourActivity:YourRecycleViewClickListener{overridefunonItemClickListener(param1:View, param2: String) {
        //do any thing
    }
}

third step make interface constract in your recycle adapter

classYourAdapter(
    private val listener: YourRecycleViewClickListener){

    holder.constraintLayout.setOnClickListener{
      listener.onItemClickListener(param1,param2)
    }

}

this is by kotlin lang and by java is same but change syntax

that all to do

Post a Comment for "How To Pass Data From A Activity To A Recycler View Adapter In Android"