I am setting this adapter for an RecyclerView, I am failing to make an indent from that adapter I don't know why can some body guide me what is going on here because I am not an expert in programming as well in this android.
Below is my Adapter class and Activity class.
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private String[] mDatasetname;
private Integer[] mexp;
private Context context;
public ToggleButton select;
Integer selectedcountint=0;
private Bitmap[] mpro;
private String[] mloc;
private String[] mobj;Context m;
private String[] mselected;
public ArrayList<String> nselected = new ArrayList<>();
public static class MyViewHolder extends RecyclerView.ViewHolder{
public CardView mCardView;
public TextView mTextView;
public TextView texp;
public Button pdf;
public ToggleButton select;
public ImageView pro;
public TextView loc;
public View layout;
Context context;
private RecyclerView.ViewHolder s;
public MyViewHolder(View v){
super(v);
mCardView = (CardView) v.findViewById(R.id.card_view);
mTextView = (TextView) v.findViewById(R.id.tv_text);
texp = (TextView) v.findViewById(R.id.setexp);
pdf = (Button) v.findViewById(R.id.moreinfo);
select = (ToggleButton) v.findViewById(R.id.select);
pro = (ImageView) v.findViewById(R.id.setpropic);
loc = (TextView) v.findViewById(R.id.setlocation);
}
void bind(String name, Integer exp, String location, final Bitmap image, final String obj ) {
mTextView.setText(name);
texp.setText(String.valueOf(exp)+" yrs");
pro.setImageBitmap(image);
loc.setText(location);
pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Dashboard.this, com.parse.starter.View.class);
intent.putExtra("no", "6382551203");
startActivity(intent);
}
});
}
}
public MyAdapter(Context s,String[] myDataset,Integer[] exp,Bitmap[] pro,String[] loc,String[] obj){
mDatasetname = myDataset;
mexp=exp;
mpro=pro;
mloc=loc;
mobj=obj;
m=s;
}
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
context=parent.getContext();
View vs = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false);
MyViewHolder vh = new MyViewHolder(vs);
return vh;
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position){
holder.bind(mDatasetname[position],mexp[position],mloc[position],mpro[position],mobj[position]);
}
#Override
public int getItemCount() { return mDatasetname.length; }
}
Solution:
Instead of this:
Intent intent = new Intent(Dashboard.this, com.parse.starter.View.class);
intent.putExtra("no", "6382551203");
startActivity(intent);
Write like this:
Intent intent = new Intent(Dashboard.this, (your_destination_class_name).class);
intent.putExtra("no", "6382551203");
startActivity(intent);
For Example:
Intent intent = new Intent(Dashboard.this, RegisterActivity.class);
intent.putExtra("no", "6382551203");
startActivity(intent);
If you're destination class name is View.class then change it to something else.
If the above method doesn't work,
Replace:
View vs = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false);
with:
View vs = LayoutInflater.from(m).inflate(R.layout.card_item, parent, false);
and try. Hope it works.
first make constructor, adapter wont work with out constructor, its a big miss please check
holder.pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,YourIndentClassName.class);
intent.putExtra("no", "6382551203");
context.startActivity(intent);
}
});
Edit your code with this:
pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, com.parse.starter.View.class/*should be a proper class name*/);
intent.putExtra("no", "6382551203");
context.startActivity(intent);
}
});
holder.pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,YourIndentClassName.class);
intent.putExtra("no", "6382551203");
context.startActivity(intent);
}
});
strong text
use this code in your onBindViewHolder method of RecyclerVieAdapter
Related
I've created an adapter for my Category gridview to show in my Main activity.
Categories are shown inside a CardView.
In order to click on each category, i can only access by the image or the text in the viewholder. that's not ideal, as you have to clicl exactly on the image to open new activity.
I would love to add the "itemview" to be able to click in any point of the cardview. I did it, but it does not work. I can only see the click but nothing happens.
Something like this:
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, categoryModel.getCatName(), Toast.LENGTH_SHORT).show();
}
});
My Adapter:
public class categoryCardViewAdpt extends RecyclerView.Adapter<categoryCardViewAdpt.ViewHolder> {
CategoryModel[] categoryModels;
Context context;
public categoryCardViewAdpt (CategoryModel[] categoryModels, MainActivity activity){
this.categoryModels = categoryModels;
this.context = activity;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView catImageView;
public TextView catTv;
public ViewHolder(#NonNull View itemView) {
super(itemView);
catImageView = (ImageView) itemView.findViewById(R.id.category_img2);
catTv = (TextView) itemView.findViewById(R.id.category_name_2);
}
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.card_view_layout,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
final CategoryModel categoryModel = categoryModels[position];
holder.catTv.setText(categoryModel.getCatName());
holder.catImageView.setImageResource(categoryModel.getCatImage());
holder.catImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Toast.makeText(context, categoryModel.getCatName(), Toast.LENGTH_SHORT).show();
if (categoryModel.getCatName().equals(R.string.farina)){
Intent intent = new Intent(context, FarinaActivity.class);
context.startActivity(intent);
}
else if (categoryModel.getCatName().equals(R.string.farina_inverso)){
Intent intent = new Intent(context, FarinaInversoActivity.class);
context.startActivity(intent);
//overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
else if (categoryModel.getCatName().equals(R.string.alcol)){
Intent intent = new Intent(context, AlcolActivity.class);
context.startActivity(intent);
}
else if (categoryModel.getCatName().equals(R.string.panna)){
Intent intent = new Intent(context, PannaActivity.class);
context.startActivity(intent);
}
else if (categoryModel.getCatName().equals(R.string.chocolate)){
Intent intent = new Intent(context, ChocoActivity.class);
context.startActivity(intent);
}
}
});
}
#Override
public int getItemCount() {
return categoryModels.length;
}
}
your holder.catImageView is in your ItemView. so the click event may intercepted by ItemView.
Do not use holder.catImageView.setOnClickListener
use : holder.itemView.setonClickListener.
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private String[] mDatasetname;
private Integer[] mexp;
Context context;
public ToggleButton select;
Integer selectedcountint=0;
private Bitmap[] mpro;
private String[] mloc;
private String[] mobj;
private String[] mselected;
public ArrayList<String> nselected = new ArrayList<>();
public static class MyViewHolder extends RecyclerView.ViewHolder{
public CardView mCardView;
public TextView mTextView;
public TextView texp;
public Button pdf;
public ToggleButton select;
public ImageView pro;
public TextView loc;
public MyViewHolder(View v){
super(v);
mCardView = (CardView) v.findViewById(R.id.card_view);
mTextView = (TextView) v.findViewById(R.id.tv_text);
texp = (TextView) v.findViewById(R.id.setexp);
pdf = (Button) v.findViewById(R.id.moreinfo);
select = (ToggleButton) v.findViewById(R.id.select);
pro = (ImageView) v.findViewById(R.id.setpropic);
loc = (TextView) v.findViewById(R.id.setlocation);
}
}
public MyAdapter(String[] myDataset,Integer[] exp,Bitmap[] pro,String[] loc,String[] obj){
mDatasetname = myDataset;
mexp=exp;
mpro=pro;
mloc=loc;
mobj=obj;
}
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
context=parent.getContext();
View vs = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false);
MyViewHolder vh = new MyViewHolder(vs);
return vh;
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position){
holder.mTextView.setText(mDatasetname[position]);
holder.texp.setText(String.valueOf(mexp[position])+" yrs");
holder.pro.setImageBitmap(mpro[position]);
holder.loc.setText(mloc[position]);
holder.select.setText("rejected");
holder.pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent();
intent = new Intent(context, PdfViewer.class);
intent.putExtra("obj",mobj[position]);
context.startActivity(intent);
}
});
holder.select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked==true) {
holder.select.setChecked(true);
holder.select.setText("selected");
nselected.remove(mobj[position]+",rejected");
nselected.add(mobj[position]+",selected");
Log.d("dei", String.valueOf(nselected));
selectedcountint=selectedcountint+1;
Intent intent = new Intent("custom-message");
// intent.putExtra("quantity",Integer.parseInt(quantity.getText().toString()));
Bundle args = new Bundle();
args.putSerializable("ARRAYLIST",(Serializable)nselected);
intent.putExtra("BUNDLE",args);
intent.putExtra("totalval",selectedcountint);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
} else {
holder.select.setChecked(false);
holder.select.setText("rejected");
nselected.remove(mobj[position]+",selected");
nselected.add(mobj[position]+",rejected");
Log.d("dei", String.valueOf(nselected));
selectedcountint=selectedcountint-1;
Intent intent = new Intent("custom-message");
// intent.putExtra("quantity",Integer.parseInt(quantity.getText().toString()));
Bundle args = new Bundle();
args.putSerializable("ARRAYLIST",(Serializable)nselected);
intent.putExtra("BUNDLE",args);
intent.putExtra("totalval",selectedcountint);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
}
});
}
#Override
public int getItemCount() { return mDatasetname.length; }
}
I have a recycler view in my app. I am having a problem. There is an item so that when I click on the item a the funtion in the click b is executed. It would be very great when you clean up the below code for me because I am new to programming, so that I won't be able to solve the problem.
if(getAdapterPosition() != RecyclerView.NO_POSITION) {
...
mobj[getAdapterPosition()]
...
}
It's better if you set your data and listeners in the ViewHolder. You should create a method in your ViewHolder class and pass there item from your list as a parameter. In this method you copy all your code for setting text and listeners. Then in onBindViewHolder you call this method.
First create a class that will be our Adapter item (you should change the names of the fields because I don't know what should they represent, this is just an example):
class AdapterItem {
private String firstString;
private Integer integerValue;
private Bitmap bitmap;
private String secondString;
private String thirdString;
// create also getters, setters for fields
}
Your adapters constructor will look something like this:
public MyAdapter(ArrayList<AdapterItem> data){
this.data = data;
}
And the onBindViewHolder method:
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position){
holder.bind(data[position]);
}
In the ViewHolder class:
void bind(AdapterItem item) {
mTextView.setText(item.getFirstString());
pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, PdfViewer.class);
intent.putExtra("obj", item.getThirdString());
v.getContext().startActivity(intent);
}
});
}
As correctly pointed out by #andrei The code to get the correct string from the obj[] array should be as shown:
public MyAdapter(Context context, String[] myDataset,Integer[] exp,Bitmap[] pro,String[] loc,String[] obj){
mContext = context; // make mContext as a variable like others
mDatasetname = myDataset;
mexp=exp;
mpro=pro;
mloc=loc;
mobj=obj;
}
holder.pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent();
intent = new Intent(context, PdfViewer.class);
intent.putExtra("obj",mobj[holder.getAdapterPosition()]);
mContext.startActivity(intent);
}
});
And similarly replace all occurrences of position with holder.getAdapterPosition() because holder.getAdapterPosition() will always give you the updated position for an item of recycler view in your code as if you update or delete items without using notifyDataSetChanged() method then the onBindViewHolder will not be called again hence the position will become inconsistent. For detailed information you can refer to this answer.
public class MyAdapter extends RecyclerView.Adapter {
private String[] mDatasetname;
private Integer[] mexp;
private Context context;
public ToggleButton select;
Integer selectedcountint=0;
private Bitmap[] mpro;
private String[] mloc;
private String[] mobj;
public Button pdf;Context m;private String[] mselected;
public ArrayList<String> nselected = new ArrayList<>();
public static class MyViewHolder extends RecyclerView.ViewHolder{
public CardView mCardView;
public TextView mTextView;
public TextView texp;
public Button pdf;
public ToggleButton select;
public ImageView pro;
public MainActivity activity;
public Context ipaset;
public TextView loc;
public View layout;
View forresumeview;
String forresume;
private RecyclerView.ViewHolder s;
public MyViewHolder(final View v) {
super(v);
mCardView = (CardView) v.findViewById(R.id.card_view);
mTextView = (TextView) v.findViewById(R.id.tv_text);
texp = (TextView) v.findViewById(R.id.setexp);
pdf = (Button) v.findViewById(R.id.moreinfo);
select = (ToggleButton) v.findViewById(R.id.select);
pro = (ImageView) v.findViewById(R.id.setpropic);
loc = (TextView) v.findViewById(R.id.setlocation);
forresumeview=v;
}
void bind(String name, Integer exp, String location, final Bitmap image, final String obj ){
mTextView.setText(name);
texp.setText(String.valueOf(exp) + " yrs");
pro.setImageBitmap(image);
loc.setText(location);
}
public void doonce(final String s){
pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View vs) {
Intent intent = new Intent(forresumeview.getContext(), PdfViewer.class);
intent.putExtra("obj", s);
forresumeview.getContext().startActivity(intent);
// Log.d("sdasd", obj);
}
});
}
}
public MyAdapter(String[] myDataset,Integer[] exp,Bitmap[] pro,String[] loc,String[] obj){
mDatasetname = myDataset;
mexp=exp;
mpro=pro;
mloc=loc;
mobj=obj;
}
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
context=parent.getContext();
View vs = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false);
MyViewHolder vh = new MyViewHolder(vs);
return vh;
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position){
holder.bind(mDatasetname[position],mexp[position],mloc[position],mpro[position],mobj[position]);
holder.doonce(mobj[position]);
}
#Override
public int getItemCount() { return mDatasetname.length; }
}
this is the answer ,,,, it works i tried it !!!! Happy coding ......
I have a recyclerview with an 30 images and on click of particular image i am opening a new activity which shows a different image.I have uploaded my images to a server and accessing the image from there. When my activity is opened on click ofrecyclerview` i am passing the position through intent so that i can know which image was clicked,then i am using switch case in my activity and load whatever image i want.I have written 30 cases of switch as i have 30 images.Is there any alternative to this.I don't want to use if and else if.
public class ModelLineUpAdapter extends RecyclerView.Adapter<ModelLineUpAdapter.MyViewHolder> {
private Context context;
private List<Bikers> bikersList;
public ModelLineUpAdapter(List<Bikers> bikersList,Context context) {
this.bikersList=bikersList;
this.context = context;
}
public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView bikesImageView;
public TextView nameTextView, priceTextView;
private Button brocheurebutton;
private final Context context;
public MyViewHolder(View itemView) {
super(itemView);
context = itemView.getContext();
bikesImageView = itemView.findViewById(R.id.bikelistitemImageview);
nameTextView = itemView.findViewById(R.id.bikelistitemname);
priceTextView = itemView.findViewById(R.id.bikelistitemprice);
brocheurebutton = itemView.findViewById(R.id.bikelistitembutton);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent(context, ModelLineUpInnerActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("position",getAdapterPosition());
context.startActivity(intent);
}
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.bikelistitem, null);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Bikers bikers = bikersList.get(position);
holder.nameTextView.setText(bikers.getBikename());
holder.priceTextView.setText(bikers.getBikeprice());
Glide.with(context).load(bikers.getBikeImageUrl()).into(holder.bikesImageView);
}
#Override
public int getItemCount() {
//Log.i(TAG, "getItemCount: "+bikersList.size());
return bikersList == null ? 0 : bikersList.size();
}
}
just pass image url with intent and recieve in your ModelLineUpInnerActivity
#Override
public void onClick(View v) {
Intent intent = new Intent(context, ModelLineUpInnerActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("position",getAdapterPosition());
intent.putExtra("IMG",bikersList.get(getAdapterPosition()).getBikeImageUrl());
context.startActivity(intent);
}
Is any way to transfer the current list of items to a new Activity using OnClickListener of ViewHolder? Or any other way?
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.EventsViewHolder> {
List<Event> events; //get this List
RVAdapter(List<Event> events) {
this.events = events;
}
public static class EventsViewHolder extends RecyclerView.ViewHolder {
TextView date;
TextView text;
ImageView photo;
EventsViewHolder(final View itemView) {
super(itemView);
date = (TextView) itemView.findViewById(R.id.date);
text = (TextView) itemView.findViewById(R.id.text);
photo = (ImageView) itemView.findViewById(R.id.photo);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context context = v.getContext();
SendObject sendObject = new SendObject(setList); //and transfer it here
Intent intent = new Intent(context, ReadEvent.class);
Bundle bundle = new Bundle();
bundle.putSerializable("events", sendObject);
bundle.putInt("pos", getAdapterPosition());
bundle.putString("test", "test");
intent.putExtras(bundle);
context.startActivity(intent);
}
});
}
}
Solution 1: Create method in ViewHolder in which you pass your list
Solution 2: Define your ViewHolder as not static class and pass your list
Another approach:
// Solution 1:
public class EventsViewHolder extends RecyclerView.ViewHolder {
EventsViewHolder(final View itemView) {
super(itemView);
}
public void bind(List<Event> events) {
...
SendObject sendObject = new SendObject(events); //and transfer it here
...
}
}
// Solution 2:
public class EventsViewHolder extends RecyclerView.ViewHolder {
EventsViewHolder(final View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SendObject sendObject = new SendObject(events); //and transfer it here
}
});
}
}
Adapter Class.
public ServicesHomeAdapter(Context context, ArrayList<HomeScreenData> homeScreenDataArrayList,OnItemClickedListener listener) {
this.context=context;
this.homeScreenDataArrayList=homeScreenDataArrayList;
this.listener=listener;
}
#Override
public ServicesHomeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_service,parent,false);
ViewHolder viewHolder=new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(final ServicesHomeAdapter.ViewHolder holder, int position) {
final HomeScreenData homeScreenData = homeScreenDataArrayList.get(position);
}
#Override
public int getItemCount()
{
return (null!=homeScreenDataArrayList?homeScreenDataArrayList.size():0);
}
public class ViewHolder extends RecyclerView.ViewHolder {
protected ImageView ivSection;
protected TextView txImageName;
public ViewHolder(View itemView)
{
super(itemView);
this.ivSection= (ImageView) itemView.findViewById(R.id.ivcard);
this.txImageName= (TextView) itemView.findViewById(R.id.txtimgname);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onItemClicked(getAdapterPosition());
}
});
}
}
public interface OnItemClickedListener{
void onItemClicked(int position);
}
}
Implement the interface in your Activity.
#Override
public void onItemClicked(int position) {
Intent intent = new Intent(getActivity(), A.class);
Pojo Class pojo = arrayList.get(position);
intent.putExtra(TAG, pojo);
startActivity(intent);
}`
Your Pojo should parseable or serialised but now android recommends to implement parseable then you can pass any Pojo through intent, How you can make your class parseable check this link.
https://developer.android.com/reference/android/os/Parcelable.html
intent.putExtra(key, Serializable/Parcelable) and then in second activity getParcelableExtra(key)/getSerializableExtra(key)
I had a Serializable object already:
public class SendObject implements Serializable{
private List<Event> events;
public SendObject(List<Event> events) {
this.events = events;
}
public List<Event> getEvents() {
return events;
}
}
The main problem was to receive current Adapter List. The problem is solved by using Singleton. The solution was simple.
public class RVAdapter extends RecyclerView.Adapter {
private static RVAdapter rvAdapter;
private List<Event> events;
public RVAdapter() {
}
public static RVAdapter getInstance() {
if (rvAdapter==null) {
rvAdapter = new RVAdapter();
}
return rvAdapter;
}
public void setList(List<Event> events) {
this.events = events;
}
public static class EventsViewHolder extends RecyclerView.ViewHolder {
TextView date;
TextView text;
ImageView photo;
EventsViewHolder(final View itemView) {
super(itemView);
date = (TextView) itemView.findViewById(R.id.date);
text = (TextView) itemView.findViewById(R.id.text);
photo = (ImageView) itemView.findViewById(R.id.photo);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context context = v.getContext();
SendObject sendObject = null;
sendObject = new SendObject(RVAdapter.getInstance().events); //current List
Intent intent = new Intent(context, ReadEvent.class);
Bundle bundle = new Bundle();
bundle.putSerializable("events", sendObject);
bundle.putInt("pos", getAdapterPosition());
intent.putExtras(bundle);
context.startActivity(intent);
}
});
}
}
Anyway, thanks for your responses!
I started in the android programming and and I encountered a problem when i tried to send data from my Adapter to an another activity
.
I want to show the content of "description" in my other activity when I click on an item.
MyAdapter class :
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private final List<FakeNews> list = FakeNewsList.all;
#Override
public int getItemCount() {
return list.size();
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.list_cell, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
FakeNews pair = list.get(position);
holder.display(pair);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private final TextView name;
private final TextView description;
private FakeNews currentPair;
public MyViewHolder(final View itemView) {
super(itemView);
name = ((TextView) itemView.findViewById(R.id.name));
description = ((TextView) itemView.findViewById(R.id.description));
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MyViewHolder.this ,Affich.class);
intent.putExtra("description", String.valueOf((description)));
startActivity(intent);
}
});
}
public void display(FakeNews pair) {
currentPair = pair;
name.setText(pair.title);
}
}
}
Can someone help me?
I assume that you do not really want to pass the TextView to your next activity, but the content which is shown in it. Also, you need to pass a Context to the Intent and startActivity() is a method of an activity, so you need to call it on the context:
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext() ,Affich.class);
intent.putExtra("description", description.getText().toString());
view.getContext().startActivity(intent);
}
});
Pass the Context or Activity instance into your adapter class.
then while starting new Activity and suppose instance variable is mContext use like this-
Intent intent = new Intent(mContext ,Affich.class);
intent.putExtra("description", String.valueOf((description)));
mContext.startActivity(intent);
To pass value from TextView to your new activity, do this:
intent.putExtra("description", description.getText().toString());
on your new activity, get the description value using:
String descriptionValue = getIntent().getStringExtra("description");