Reputation: 4226
Currently I have a RecyclerView, that opens Activity2
/activity2.xml
. activity2.xml
has a textview.
MY QUESTION IS: How can I set the text in activity2.xml
depending on what item was clicked in my recyclerview? I also want to set a image but that can wait for now.
I'm using a RecyclerView.ViewHolder
that implements a View.OnClickListener
.
Here is my RecyclerView.Adapter
RecyclerPracticeAdapter.java
public class RecyclerPracticeAdapter extends RecyclerView.Adapter<MyViewHolder> {
Context c;
ArrayList<Category> category;
public RecyclerPracticeAdapter(Context c, ArrayList<Category> category) {
this.c = c;
this.category = category;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.model, null);
MyViewHolder holder = new MyViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.nameTxt.setText(category.get(position).getName());
holder.img.setImageResource(category.get(position).getImage());
//Listners
//THIS IS WHERE YOU CAN CHOOSE TO OPEN A ACTIVITY WHEN USER CLICK ON A CARDVIEW!!
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(View v, int pos) {
switch (pos) {
case 0:
// Open New Activity......
// THIS IS WHERE I WANT TO CHANGE TEXT activity2.xml
Intent intent=new Intent(v.getContext(),Activity2.class);
v.getContext().startActivity(intent);
break;
case 1:
Intent intent=new Intent(v.getContext(),Activity2.class);
v.getContext().startActivity(intent);
break;
}
}
});
}
@Override
public int getItemCount() {
return category.size();
}
}
activity2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Update Text"
android:id="@+id/txt_update"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal" />
</RelativeLayout>
Can someone please point me in the right direction?
EDIT:::
MyViewHolder:
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView img;
public TextView nameTxt;
ItemClickListener itemClickListener;
public MyViewHolder(View itemView) {
super(itemView);
nameTxt= (TextView) itemView.findViewById(R.id.nameTxt);
img= (ImageView) itemView.findViewById(R.id.ppImage);
itemView.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener ic){
this.itemClickListener=ic;
}
@Override
public void onClick(View v) {
this.itemClickListener.onItemClick(v,getLayoutPosition());
}
}
Category:
public class Category {
private int name;
private int image;
public Category(int name, int image) {
this.name = name;
this.image = image;
}
public int getName() {
return name;
}
public void setName(int name) {
this.name = name;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
}
Upvotes: 0
Views: 623
Reputation: 294
You just need to pass the data
Intent intent=new Intent(v.getContext(),Activity2.class);
intent.putExtra("value",category.get(pos).getName());
v.getContext().startActivity(intent);
and in Activity2 you need to take the value
String updatevalue=getIntent().getExtras().getString("value"); TextView.setText(updatevalue);
Upvotes: 0
Reputation: 4907
1.Pass the data with intent in onClick of item.
Intent intent=new Intent(v.getContext(),Activity2.class);
intent.putExtra("YOUR_KEY", category.get(pos).getName());
v.getContext().startActivity(intent);
2.Get the data in your Activity2
.
Bundle bundle = getIntent().getExtras();
if(bundle!=null){
String text = bundle.getString("YOUR_KEY");
if(text!=null)
YOUR_TEXT_VIEW.setText(text);
}
Upvotes: 2