Issue
all devlopers
See i want to go in other activity which shows the description of clicked item in recycler view ;I know how to do ....we can do by Intent put extra. But my problem is we can put extra those all data which lies on cardview but i want to send data to another activity which is not in the cardview of item of recycler view .like discription of item So how to do ,please can anyone explain me? Thank you
Solution
I think you had set event navigate to another activity (startActivity
) inside adapter.
You should let startActivity
in your activity class, and when recyclerview item click, just invoke callback.
Example:
- Create your interface
public interface YourListener {
/**
* This method will invoke when button of recyclerview item click
**/
void navigateToAnotherActivityWithData(String data);
}
- Put listener event to your adapter
class YourAdapter extends RecyclerView.Adapter<ViewHolder> {
private YourListener mListener;
private String dataOfRecyclerView;
public YourAdapter(YourListener listener) {
this.mListener = listener;
}
...
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.btnNext.setOnClickListener(v-> {
if(mListener != null)
mListener.navigateToAnotherActivityWithData(dataOfRecyclerView);
});
...
}
...
}
- Define event action in your activity. And you can send data from both recyclerview and activity.
adapter = new YourAdapter(data -> {
Intent intent = Intent(this, AnotherActivity.class);
intent.putExtra("DATA_OF_RECYCLERVIEW", data);
intent.putExtra(...);
...
});
Answered By - Loc Le Xuan
Answer Checked By - Mary Flores (JavaFixing Volunteer)