Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RecipeListAdapter extends RecyclerView.Adapter<RecipeListAdapter.ViewHolder>{
- private List<Recipe> mRecipeSet;
- private Button mAddToGroceriesButton;
- public RecipeListAdapter(List<Recipe> recipes){
- mRecipeSet = recipes;
- }
- // Provide a reference to the views for each data item
- // Complex data items may need more than one view per item, and
- // you provide access to all the views for a data item in a view holder
- public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
- //This is what will handle what happens when you click a recipe in the recycler view
- private TextView mRecipeName;
- private TextView mPrepTime;
- private TextView mCookTime;
- private TextView mServingSize;
- private RelativeLayout mRecipeTextSection;
- public ViewHolder(View v) {
- super(v);
- mRecipeName = (TextView) v.findViewById(R.id.recipe_list_recycler_view_recipe_name);
- mServingSize = (TextView) v.findViewById(R.id.recipe_list_recycler_view_serving_size);
- mPrepTime = (TextView) v.findViewById(R.id.recipe_list_recycler_view_prep_time);
- mCookTime = (TextView) v.findViewById(R.id.recipe_list_recycler_view_cook_time);
- mRecipeTextSection = (RelativeLayout) v.findViewById(R.id.recycled_item_section_view);
- mRecipeTextSection.setOnClickListener(this);
- mAddToGroceriesButton = (Button) v.findViewById(R.id.add_to_grocery_list);
- mAddToGroceriesButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- int position = getAdapterPosition();
- Recipe recipeToGrocery = mRecipeSet.get(position);
- //RecipeDB dbHelper = new RecipeDB(v.getContext());
- //dbHelper.addGroceryItem(recipeToGrocery);
- if(!recipeToGrocery.isInList()) {
- RecipeDB dbHelper = new RecipeDB(v.getContext());
- dbHelper.addGroceryItem(recipeToGrocery);
- recipeToGrocery.setInList(true);
- dbHelper.updateRecipe(recipeToGrocery);
- mAddToGroceriesButton.setBackgroundResource(R.mipmap.ic_playlist_add_check_black_24dp);
- Toast.makeText(v.getContext(), recipeToGrocery.getRecipeName() + " added to grocery list.", Toast.LENGTH_SHORT).show();
- }
- else {
- Toast.makeText(v.getContext(), "That recipe is already in the list.", Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- @Override
- public void onClick(View v){
- int position = getAdapterPosition();
- Intent i = new Intent(v.getContext(), RecipeTextView.class);
- Recipe selectedRecipe = mRecipeSet.get(position);
- i.putExtra("view_recipe_key", selectedRecipe);
- v.getContext().startActivity(i);
- }
- }
- public void add(int position, Recipe item) {
- mRecipeSet.add(position, item);
- notifyItemInserted(position);
- }
- public void remove(Recipe item) {
- int position = mRecipeSet.indexOf(item);
- mRecipeSet.remove(position);
- notifyItemRemoved(position);
- }
- public RecipeListAdapter(ArrayList<Recipe> myRecipeset) {
- mRecipeSet = myRecipeset;
- }
- // Create new views (invoked by the layout manager)
- @Override
- public RecipeListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- // create a new view
- View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recipe_item_recycled, parent, false);
- ViewHolder vh = new ViewHolder(v);
- return vh;
- }
- @Override
- public void onBindViewHolder(ViewHolder holder, int position) {
- Recipe recipe = mRecipeSet.get(position);
- String recipeName = recipe.getRecipeName();
- String prepTime = "Prep Time: " + String.valueOf(recipe.getPrepTime()) + " minutes";
- String cookTime = "Cook Time: " + String.valueOf(recipe.getCookTime()) + " minutes";
- String servingSize = "Servings: " + String.valueOf(recipe.getServings());
- holder.mRecipeName.setText(recipeName);
- //Only display values if they are not null
- if(recipe.getServings() != null) {
- holder.mServingSize.setText(servingSize);
- }
- if (recipe.getPrepTime() != null) {
- holder.mPrepTime.setText(prepTime);
- }
- if(recipe.getCookTime() != null) {
- holder.mCookTime.setText(cookTime);
- }
- }
- // Return the size of your dataset (invoked by the layout manager)
- @Override
- public int getItemCount() {
- if(mRecipeSet != null) {
- return mRecipeSet.size();
- }
- return 0;
- }
- }
- mAddToGroceriesButton.setBackgroundResource(R.mipmap.ic_playlist_add_check_black_24dp);
- if(recipe.isInList()){
- mAddToGroceriesButton.setBackgroundResource(R.mipmap.ic_playlist_add_check_black_24dp);
- }
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/apk/res/android"
- android:layout_margin="7dp"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:paddingLeft="10dp"
- android:id="@+id/recycled_item_section_view"
- android:elevation="30dp"
- android:background="@drawable/background_border"
- >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- tools:text="Recipe name"
- android:textSize="24dp"
- android:textColor="@color/black"
- android:id="@+id/recipe_list_recycler_view_recipe_name"
- android:paddingBottom="3dp"
- android:maxWidth="275dip"
- android:singleLine="false"/>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="18dp"
- android:textColor="@color/black"
- android:layout_below="@id/recipe_list_recycler_view_recipe_name"
- android:id="@+id/recipe_list_recycler_view_serving_size"
- android:paddingBottom="3dp"/>
- <Button
- android:layout_width="35dp"
- android:layout_height="35dp"
- android:background="@mipmap/ic_playlist_add_black_24dp"
- android:height="36dp"
- android:padding="8dp"
- android:layout_alignParentRight="true"
- android:id="@+id/add_to_grocery_list"
- android:layout_alignParentTop="true"
- android:layout_alignParentEnd="true"
- />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/recipe_list_recycler_view_serving_size"
- android:layout_alignParentLeft="true"
- android:textSize="18dp"
- android:textColor="@color/black"
- android:id="@+id/recipe_list_recycler_view_prep_time"
- android:paddingBottom="3dp"
- />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/recipe_list_recycler_view_prep_time"
- android:textSize="18dp"
- android:textColor="@color/black"
- android:layout_alignParentLeft="true"
- android:id="@+id/recipe_list_recycler_view_cook_time"/>
- </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement