I've created a RecyclerView that gets inflated with an ArrayList of Strings and I want to put extra information in an intent for the next activity to get the information. I can pass the position of the element in the reclyclerview(within the intent), but I also want to send the string value that appears in that element. The layout used by the adapter for the recylcerView has nothing but a TextView for displaying the values in the ArrayList of strings.
The problem is in the Adapter, because I want to be able to put an String as extra information in the intent. And what I have to change is the onItemClick method parameters inside ItemClickListener, but I don't know what to do next.
Here's the Adapter:
Adapter_dishes_rv.java
public class Adapter_dishes_rv extends RecyclerView.Adapter<Adapter_dishes_rv.ViewHolder> {
private List<String> mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
Adapter_dishes_rv(Context context, List<String> data) {
this.mInflater = LayoutInflater.from(context);
this.mData = data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.recyclerview_simple_layout, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String animal = mData.get(position);
holder.myTextView.setText(animal);
}
@Override
public int getItemCount() {
return mData.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView myTextView;
ViewHolder(View itemView) {
super(itemView);
myTextView = itemView.findViewById(R.id.nameoftypeofdish);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
String getItem(int id) {
return mData.get(id);
}
void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}
I think that all I have to change is just the OnItemClick parameters but then what else?
Here's the layout just in case:
recyclerview_simple_layout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/nameoftypeofdish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
</LinearLayout>
And then there's the main Activity:
FoodFamilyActivity.java
public class FoodFamilyActivity extends Activity implements Adapter_dishes_rv.ItemClickListener {
Adapter_dishes_rv adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_family);
ArrayList<String> dishesOfAFamily = new String[]{"Sopa de pobre", "Sopa de pescado", "Sopa de sobre"}
RecyclerView recyclerView = findViewById(R.id.FamilyDishes);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new Adapter_dishes_rv(this, dishesOfAFamily);
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
}
@Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(FoodFamilyActivity.this, DishActivity.class);
intent.putExtra(DishActivity.EXTRA_DISHNO, (int) position);
startActivity(intent);
}
}
If you need anything else tell me, because I really need help with this problem.
You can add the following function to your adapter
You are already getting the concerned position using
getAdapterPosition(). Using the above function you can get the string from the list .Change your interface to
Now you have the required string in your activity where you can pass it to Intent