I'm trying to figure how to go to another activity when I click on one item of a cardview (with recyclerview).
I searched on internet and this website and tried several options, but it isn't working (I tried only for position 0, the others will be done the same). Here, with the card 0 i try to go to "selection_niveau".
1st try : inside OnClick:
public class adapter_categorie_solo extends RecyclerView.Adapter<adapter_categorie_solo.ViewHolder> {
List<String> titles;
List<Integer> images;
LayoutInflater inflater;
public adapter_categorie_solo(Context ctx, List<String> titles, List<Integer> images){
this.titles = titles;
this.images = images;
this.inflater = LayoutInflater.from(ctx);
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.cardview_solo_categorie,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.title.setText(titles.get(position));
holder.gridIcon.setImageResource(images.get(position));
}
#Override
public int getItemCount() {
return titles.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView title;
ImageView gridIcon;
public ViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.textView2);
gridIcon = itemView.findViewById(R.id.imageView2);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Clicked -> " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
if (getAdapterPosition()==0)
Intent intent = new Intent (this,selection_niveau.class);
}
});
}
}
}
I get this error :
error: variable declaration not allowed here
Intent intent = new Intent (this,selection_niveau.class);
2nd try : inside onBindViewHolder
public class adapter_categorie_solo extends RecyclerView.Adapter<adapter_categorie_solo.ViewHolder> {
List<String> titles;
List<Integer> images;
LayoutInflater inflater;
public adapter_categorie_solo(Context ctx, List<String> titles, List<Integer> images){
this.titles = titles;
this.images = images;
this.inflater = LayoutInflater.from(ctx);
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.cardview_solo_categorie,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.title.setText(titles.get(position));
holder.gridIcon.setImageResource(images.get(position));
Intent intent;
if(position == 0)
intent = new Intent(this, selection_niveau.class);
holder.itemView.setOnClickListener(new View.OnClickListener(){
#Override public void onClick(View v){
Context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return titles.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView title;
ImageView gridIcon;
public ViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.textView2);
gridIcon = itemView.findViewById(R.id.imageView2);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Clicked -> " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
}
});
}
}
}
And I get this error :
error: no suitable constructor found for Intent(adapter_categorie_solo,Class<selection_niveau>)
intent = new Intent(this, selection_niveau.class);
^
constructor Intent.Intent(String,Uri) is not applicable
(argument mismatch; adapter_categorie_solo cannot be converted to String)
constructor Intent.Intent(Context,Class<?>) is not applicable
(argument mismatch; adapter_categorie_solo cannot be converted to Context)
I tried to change "this" to only "context", but it doesn't work either.
These are some of the solutions I found on internet and here, but none is working and I can't really understand why as being a beginner.
Inside the onClick() callback this keyword will points to the anonymous inner class of the View.OnClickListener; so you need to change to a context by using v.getContext() instead.
Apply this by replacing itemView.setOnClickListener with:
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Clicked -> " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
if (getAdapterPosition()==0) {
Intent intent = new Intent (v.getContext(), selection_niveau.class);
}
}
});
Intent constructor requires a context as the first argument, but this in the ViewHolder refers to the instance of ViewHolder, So you need to get context from the view or use the context reference you got in the constructor of the adapter_categorie_solo.
public class ViewHolder extends RecyclerView.ViewHolder{
TextView title;
ImageView gridIcon;
public ViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.textView2);
gridIcon = itemView.findViewById(R.id.imageView2);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Clicked -> " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
if (getAdapterPosition()==0)
{
Intent intent = new Intent (ctx,selection_niveau.class);
ctx.startActivity(intent);
}
}
});
}
}
Using switch instead of if will be an optimal approach if you want to start different activity based on click position.
use context in Intent instead of this
if(position == 0)
intent = new Intent(context, selection_niveau.class);
Related
I have a RecyclerView that shows a list of student details. The issue I face is when I click on the first item it returns the data of the second item.
I believe that it has something to do with getAdapterPosition(), but I'm not sure.
In this case, it returns the Roll No. of the second student(item) when the first student is selected.
Any help is appreciated
RecyclerViewAdapter
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {
Context context;
public List<StudentDataModel> dataModels;
public StudentDataModel dataAdapter;
public StudentAdapter(List<StudentDataModel> getDataAdapter, Context context){
super();
this.dataModels = getDataAdapter;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.student, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(final StudentAdapter.ViewHolder viewHolder, final int position) {
dataAdapter = dataModels.get(position);
viewHolder.NameStudentCard.setText(dataAdapter.getName());
viewHolder.RankStudentCard.setText(dataAdapter.getRank());
viewHolder.RollStudentCard.setText(dataAdapter.getRoll());
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
notifyDataSetChanged();
Toast.makeText(v.getContext(), "Tapped on" + dataAdapter.getRoll(), Toast.LENGTH_SHORT).show(); //Here it returns the Roll No. of the second student(item) in the recyclerView.
Intent intent = new Intent (v.getContext(), StudentDetailsActivity.class);
v.getContext().startActivity(intent);
}
});
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView NameStudentCard;
public TextView RankStudentCard;
public TextView RollStudentCard;
public ViewHolder(View itemView) {
super(itemView);
NameStudentCard=(TextView)itemView.findViewById(R.id.textViewName);
RankStudentCard=(TextView)itemView.findViewById(R.id.textViewRank);
RollStudentCard =(TextView)itemView.findViewById(R.id.textViewEmployeeID);
}
}
#Override
public int getItemCount() {
return dataModels.size();
}
}
Yes, because you are declaring object as global object. So it is getting update with last visible row's object, because onBindViewHolder() gets called for each visible row sequentially. So it will work properly only for last visible row.
So you need to declare it inside method :
comment this :
// public StudentDataModel dataAdapter;
declare it here :
#Override
public void onBindViewHolder(final StudentAdapter.ViewHolder viewHolder, final int position) {
final StudentDataModel dataAdapter = dataModels.get(position); // add this here
}
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {
Context context;
public List<StudentDataModel> dataModels;
public StudentAdapter(List<StudentDataModel> getDataAdapter, Context context){
super();
this.dataModels = getDataAdapter;
this.context = context;
}
public StudentDataModel dataAdapter;
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.student, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(final StudentAdapter.ViewHolder viewHolder, final int position) {
dataAdapter = dataModels.get(position);
viewHolder.NameStudentCard.setText(dataAdapter.getName());
viewHolder.RankStudentCard.setText(dataAdapter.getRank());
viewHolder.RollStudentCard.setText(dataAdapter.getRoll());
}
#Override
public int getItemCount() {
return dataModels.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView NameStudentCard;
public TextView RankStudentCard;
public TextView RollStudentCard;
public ViewHolder(View itemView) {
super(itemView);
NameStudentCard=(TextView)itemView.findViewById(R.id.textViewName);
RankStudentCard=(TextView)itemView.findViewById(R.id.textViewRank);
RollStudentCard =(TextView)itemView.findViewById(R.id.textViewEmployeeID);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Tapped on" + dataModels.get(getAdapterPosition()).getRoll(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent (v.getContext(), StudentDetailsActivity.class);
v.getContext().startActivity(intent);
}
});
}
}
}
Try this code..
make interface into adapter class for click handling like this way..
onItemClickListner onItemClickListner;
public void setOnItemClickListner(RecyclerViewAdpater.onItemClickListner onItemClickListner) {
this.onItemClickListner = onItemClickListner;
}
public interface onItemClickListner{
void onItemClick(String data); // pass any data;
}
#Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
// below code handle click event on recycler view item.
String data=mStringList.get(position);
holder.textView.setText(data);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onItemClickListner.onItemClick(data); //pass your data;
}
});
}
after that bind adapter into recyclerview and adapter not null then called below code..
recyclerViewAdpater.setOnItemClickListner(new RecyclerViewAdpater.onItemClickListner() {
#Override
public void onItemClick(String data) {
// perform your operation.
recyclerViewAdpater.notifyDataSetChanged();
}
});
I'm fetching RSS feed into my APP and trying to open an intent(Web browser) when a one of the news of recyclerView is clicked, but it dosn't work. and that's my code:
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<FeedItem> feedItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
//Call Read rss asyntask to fetch rss
ReadRss readRss = new ReadRss(this, recyclerView);
readRss.execute();
recyclerView.setOnClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
**FeedItem current=feedItems.get(position);
Uri uri = Uri.parse(current.getLink());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);**
}
});
}
}
I did even try to put the same code in my holder class but in there the startActivity() method don't work:
the above code is kinda the same but in another class:
public class FeedsAdapter extends RecyclerView.Adapter<FeedsAdapter.MyViewHolder> implements View.OnClickListener {
ArrayList<FeedItem> feedItems;
Context context;
public FeedsAdapter(Context context, ArrayList<FeedItem>feedItems){
this.feedItems=feedItems;
this.context=context;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(context).inflate(R.layout.custum_row_news_item,parent,false);
view.setClickable(true);
view.setOnClickListener(this);
MyViewHolder holder=new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
YoYo.with(Techniques.FadeIn).playOn(holder.cardView);
FeedItem current=feedItems.get(position);
holder.Title.setText(current.getTitle());
holder.Description.setText(current.getDescription());
holder.Date.setText(current.getPubDate());
Picasso.get().load(current.getThumbnailUrl()).into(holder.Thumbnail);
holder.cardView.setTag(position);
Toast.makeText(context,current.getLink() ,Toast.LENGTH_SHORT).show();
}
#Override
public int getItemCount() {
return feedItems.size();
}
#Override
public void onClick(View view) {
**int position = (int) view.getTag();
FeedItem current=feedItems.get(position);
Toast.makeText(context,"The Item Clicked is: "+current.getLink() ,Toast.LENGTH_SHORT).show();
Uri uri = Uri.parse(current.getLink());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);**
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView Title,Description,Date;
ImageView Thumbnail;
CardView cardView;
public MyViewHolder(View itemView) {
super(itemView);
Title= (TextView) itemView.findViewById(R.id.title_text);
Description= (TextView) itemView.findViewById(R.id.description_text);
Date= (TextView) itemView.findViewById(R.id.date_text);
Thumbnail= (ImageView) itemView.findViewById(R.id.thumb_img);
cardView= (CardView) itemView.findViewById(R.id.cardview);
}
}
}
this code:
FeedItem current=feedItems.get(position);
Uri uri = Uri.parse(current.getLink());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
is the same code that I tried in two classes in the first the error is because of RecyclerView, And in the second one it gives error because startActivity don't work in that class.
So how do I activate the clicakble news that when clicked goes to the browser?
To start activity from within the adapter class use
context.startActivity(intent);
( if thats your question.)
My suggestion
Set the click listener in onBindViewHolder(..) of your adapter on holder.itemView.setOnClick....
I am calling an activity and also transferring the values from adapter to the next activity the values are transferred but unable to star the activity and also there is no error just the next activity doesn't open
Holder:
public class ContactReadHolder extends RecyclerView.ViewHolder {
TextView contactName, contactPhone;
ImageButton contactCall;
public ContactReadHolder(View itemView) {
super(itemView);
context =itemView.getContext();
contactName = (TextView) itemView.findViewById(R.id.contact_list_name);
contactPhone = (TextView) itemView.findViewById(R.id.contact_list_phone);
contactCall = (ImageButton) itemView.findViewById(R.id.contact_list_call);
}
}
This s my adapter code and i have implented listner in the onBindViewHolder but still it soesn't work and also i have tried in Holder class also
Adapter:
public class ContactReadAdapter extends RecyclerView.Adapter<ContactReadAdapter.ContactReadHolder> {
private LayoutInflater layoutInflater;
private List<ContactListModel> contactListModelList;
private Context context;
public ContactReadAdapter(List<ContactListModel> contactListModelList, Context context) {
this.contactListModelList = contactListModelList;
this.context = context;
}
#NonNull
#Override
public ContactReadHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.abc_contact_layout, parent, false);
ContactReadHolder contactReadHolder = new ContactReadHolder(view);
return contactReadHolder;
}
#Override
public void onBindViewHolder(#NonNull final ContactReadHolder holder, int position) {
final TextView contactName, contactPhone;
contactName = holder.contactName;
contactPhone = holder.contactPhone;
contactName.setText(contactListModelList.get(position).getModel_contact_name());
contactPhone.setText(contactListModelList.get(position).getModel_contact_phone());
holder.contactCall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("duit","clicked ");
Intent intent=new Intent(context,OutgoingCallActivity.class);
String string_contactName=holder.contactName.getText().toString();
String string_contactPhone=holder.contactPhone.getText().toString();
Log.d("duit","clicked " + string_contactName +" "+ string_contactPhone);
intent.putExtra("user_name",string_contactName);
intent.putExtra("calleeNumber", string_contactPhone);
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return contactListModelList.size();
}
Try this
Intent intent=new Intent(v.getContext(),OutgoingCallActivity.class);
// put extra methods
v.getContext().startActivity(intent);
Write your onclicklistener on onBindViewHolder function and it will work.
Intent intent=new Intent(contactName.getContext(),OutgoingCallActivity.class);
String string_contactName=holder.contactName.getText().toString();
String string_contactPhone=holder.contactPhone.getText().toString();
Log.d("duit","clicked " + string_contactName +" "+ string_contactPhone);
intent.putExtra("user_name",string_contactName);
intent.putExtra("calleeNumber", string_contactPhone);
contactName.getContext().startActivity(intent);
or
Intent intent=new Intent(holder.contactName.getContext(),OutgoingCallActivity.class);
String string_contactName=holder.contactName.getText().toString();
String string_contactPhone=holder.contactPhone.getText().toString();
Log.d("duit","clicked " + string_contactName +" "+ string_contactPhone);
intent.putExtra("user_name",string_contactName);
intent.putExtra("calleeNumber", string_contactPhone);
holder.contactName.getContext().startActivity(intent);
I'm looking to build a program where someone will click from a list of text and this will take them to another activity.
I'm aware that RecyclerView doesn't have a listener for clicks, like ListView but, after several attempts, I am unable to get this to work.
Below is the code I have attempted to implement:
MyChatRecyclerViewAdapter.java
public class MyChatRecyclerViewAdapter extends RecyclerView.Adapter<MyChatRecyclerViewAdapter.ViewHolder> {
private final List<Chat> mValues;
private final OnItemClickListener listener;
//private final OnListFragmentInteractionListener mListener;
public MyChatRecyclerViewAdapter(List<Chat> items, OnItemClickListener listener) {
this.mValues = items;
//mListener = listener;
this.listener = listener;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_chat, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mIdView.setText(mValues.get(position).id);
holder.bind(mValues.get(position), listener);
holder.mContentView.setText(mValues.get(position).content);
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (null != listener) {
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
listener.onListFragmentInteraction(holder.mItem);
}
}
});
}
#Override
public int getItemCount() {
return mValues.size();
}
public interface OnItemClickListener {
//public void onItemClick(View view , int position);
void onItemClick(Chat item);
}
public class ViewHolder extends RecyclerView.ViewHolder{
public final View mView;
public final TextView mIdView;
public final TextView mContentView;
public Chat mItem;
OnItemClickListener mItemClickListener;
/*#Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(v, getPosition());
}
}*/
public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
mIdView = (TextView) itemView.findViewById(R.id.id);
mContentView = (TextView) itemView.findViewById(R.id.content);
}
public void bind(final Chat item, final OnItemClickListener listener){
itemView.setOnClickListener(new View.OnClickListener(){
#override
public void onClick(View v){
listener.onItemClick(item);
}
});
}
#Override
public String toString() {
return super.toString() + " '" + mContentView.getText() + "'";
}
}
}
This is the tutorial I have attempted to follow: https://antonioleiva.com/recyclerview-listener/
Inside a RecyclerView, views are recycled to improve performance. You shouln't place the OnClickListener inside the onBindViewHolder as the position will not always be correct.
Try moving the OnClickListener inside the constructor of the ViewHolder and getting the current position by calling getAdapterPosition() :
public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
mIdView = (TextView) itemView.findViewById(R.id.id);
mContentView = (TextView) itemView.findViewById(R.id.content);
mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final int position = getAdapterPosition();
if (null != listener) {
listener.onListFragmentInteraction(mValues.get(position));
}
}
});
}
add following method to your MyChatRecyclerViewAdapter :
public void setClickListener(OnItemClickListener listener) {
this.listener = listener;
}
and call
mAdapter.setClickListener(new MyChatRecyclerViewAdapter.OnItemClickListener() {
#Override
public void onItemClick(Chat chat) {
}
});
from your Activity/Fragment class
You are setting onclick listener 2 times. 1 in onBind() and 1 in View holder.
Remove from on Bind() and in view holder use listener object to pass event to specific listener.
Layout.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View view) {
if (getListener() != null) {
getListener().onLayoutClick(view, this.getAdapterPosition());
}
}
});
public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
//inflate the view
View view =LayoutInflator.from(parent.getContext()).inflate(R.layout.layoutID,null);
ViewHolder holder = new ViewHolder(view);
//here we can set onClicklistener
view.setOnClickListener(new View.OnClickListeener(){
public void onClick(View v)
{
//Your action stuff
}
});
return holder;
public class ReactiveAdapter extends
RecyclerView.Adapter<MyAdapter.ViewHolder> {
String[] mDataset = { "Data", "In", "Adapter" };
private final PublishSubject<String> onClickSubject = PublishSubject.create();
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
final String element = mDataset[position];
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClickSubject.onNext(element);
}
});
}
public Observable<String> getPositionClicks(){
return onClickSubject.asObservable();
}
}
I want to pass data from onclick recycler view main activity to other activity(exoplayer).
I created onimageclicklistener class and then I declared my onimageclicklistener in the adapter class. Then I implement that listener in main activity and implemented on image click method, now I am blank and do not know what do.
I will appreciate if someone helps.
interface class:
public interface OnImageClickListener
{
void onImageClick(String imageData);
}
adapter:
public class SongAdapter extends RecyclerView.Adapter {
private Context context;
private List<Datum> datums;
//imagelistener define
private OnImageClickListener onImageClickListener;
//3rd argument is listener
public SongAdapter(Context context, List<Datum> datums, OnImageClickListener onImageClickListener) {
this.context = context;
this.datums = datums;
this.onImageClickListener = onImageClickListener;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
Datum datum = datums.get(position);
Glide.with(context).load(datums.get(position).getThumbnail()).into(holder.thumbnail);
holder.title.setText(datum.getTitle());
holder.artist.setText(datum.getArtist());
// holder.duration.setText(datum.getDuration());
holder.duration.setText(Integer.toString(datum.getDuration()));
//imageview interface pass
holder.thumbnail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onImageClickListener.onImageClick(datum.getUrl());
}
});
}
#Override
public int getItemCount() {
return datums.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView thumbnail;
TextView title;
TextView artist;
TextView duration;
public ViewHolder(#NonNull View itemView) {
super(itemView);
thumbnail = (ImageView) itemView.findViewById(R.id.thumbnail);
title = (TextView) itemView.findViewById(R.id.title);
artist = (TextView) itemView.findViewById(R.id.artist);
duration = (TextView) itemView.findViewById(R.id.duration);
}
}
}
Mainactivity:
//imageclick listener
#Override
public void onImageClick(String imageData) {
Intent i = new Intent(MainActivity.this,exoplayer.class);
startActivity(i);
}
I want to send data to exoplayer class.
Using Intent Extras
#Override
public void onImageClick(String imageData) {
Intent i = new Intent(MainActivity.this,exoplayer.class);
intent.putExtra("YOUR_KEY_STRING", imageDate);
startActivity(i);
}
And then from exoplayer Activity, you retrieve it in the onCreate() method as follows :
String imageData = getIntent().getStringExtra("YOUR_KEY_STRING");
Using bundle
#Override
public void onImageClick(String imageData) {
Intent i = new Intent(MainActivity.this,exoplayer.class);
Bundle bundle = new Bundle();
bundle.putString("imageDataKey", imageData);
intent.putExtra("YOUR_BUNDLE_KEY", bundle);
startActivity(i);
}
And then from your exoactivity you retrieve it as follows :
if (getIntent().getExtras() != null) {
Bundle bundle = getIntent().getStringExtra("YOUR_BUNDLE_KEY");
String imageData = bundle.getString("imageDataKey");
}
Using intent we can send data
//imageclick listener
#Override
public void onImageClick(String imageData) {
Intent i = new Intent(MainActivity.this,exoplayer.class);
i.putExtra("data",imageData);
startActivity(i);
}