I am developing a recipes app. I want to make it so that clicking Breakfast Entrees goes to Breakfast Entrees Activity, clicking Desserts goes to Desserts Activity, clicking Drinks goes to Drinks Activity, etc. Right now, clicking any of the recipe categories takes me to Breakfast Entrees Activity.
I included below the RecipeCategories class as well as RecipeCategoriesAdapter class.
package app.android.easyvegan;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import android.view.Menu;
import android.view.MenuItem;
public class RecipeCategories extends AppCompatActivity implements RecipeCategoriesSelectionListener {
public void onCreate(Bundle savedInstanceState) {
Utilities.SetTheme(getSharedPreferences("Themes", 0).getInt("theme", 0));
Utilities.onActivityCreateSetTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.recipe_categories);
setSupportActionBar(findViewById(R.id.topToolbar));
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
RecyclerView recyclerView = findViewById(R.id.recipeCategoriesRecycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
List<String> recipeCategoriesList = Arrays.asList(getResources().getStringArray(R.array.recipe_categories));
RecipeCategoriesAdapter adapter = new RecipeCategoriesAdapter(recipeCategoriesList, this);
recyclerView.setAdapter(adapter);
}
public boolean onCreateOptionsMenu (Menu menu){
getMenuInflater().inflate(R.menu.menu_recipe_categories, menu);
return true;
}
public boolean onOptionsItemSelected (MenuItem item){
SharedPreferences.Editor editor = getSharedPreferences("Themes", 0).edit();
switch (item.getItemId()) {
case 2131296477:
startActivity(new Intent(this, HomeScreen.class));
break;
case 2131296666:
Intent intent = new Intent("android.intent.action.SEND");
intent.setType("text/plain");
intent.putExtra("android.intent.extra.SUBJECT", "EasyVegan Recipes");
intent.putExtra("android.intent.extra.TEXT", "EasyVegan: Plant-Based Recipes App");
startActivity(Intent.createChooser(intent, "Share using"));
break;
case 2131296782:
Utilities.changeToTheme(this, 0);
editor.putInt("theme", 0);
editor.apply();
break;
case 2131296780:
Utilities.changeToTheme(this, 1);
editor.putInt("theme", 1);
editor.apply();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onRecipeCategorySelected(String recipeCategory)
{
Intent intent = new Intent(this, BreakfastEntrees.class);
startActivity(intent);
}
}
__________________________________________________________________________________
package app.android.easyvegan;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class RecipeCategoriesAdapter extends RecyclerView.Adapter<RecipeCategoriesAdapter.RecipeCategoriesViewHolder> {
private final List<String> recipeCategoriesList;
private final RecipeCategoriesSelectionListener selectionListener;
public RecipeCategoriesAdapter(List<String> recipeCategoriesList, Context context) {
this.recipeCategoriesList = recipeCategoriesList;
selectionListener = (RecipeCategories) context;
}
@NonNull
@Override
public RecipeCategoriesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
TextView textView = (TextView) LayoutInflater.from(parent.getContext()).inflate(R.layout.recipe_categories_recycler,parent,false);
return new RecipeCategoriesViewHolder(textView);
}
@Override
public void onBindViewHolder(@NonNull RecipeCategoriesViewHolder holder, int position) {
String recipeCategory = recipeCategoriesList.get(position);
holder.recipeCategory.setText(recipeCategory);
holder.recipeCategory.setOnClickListener(view -> selectionListener.onRecipeCategorySelected(recipeCategoriesList.get(holder.getAdapterPosition())));
}
@Override
public int getItemCount() {
return recipeCategoriesList.size();
}
public static class RecipeCategoriesViewHolder extends RecyclerView.ViewHolder
{
public TextView recipeCategory;
public RecipeCategoriesViewHolder (@NonNull TextView itemView) {
super(itemView);
recipeCategory = itemView;
}
}
}
I’ve tried adding a ‘when’ statement in the onBindViewHolder for each position, but if that is the solution, I don’t know how to correctly code that.
I know in the OnRecipeCategorySelcted I have it coded to start only BreakfastEntreesActivity. I’m just not sure how to adjust that to start a different Activity for each Recipe Category.
David is Awake is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.