Skip to content

Commit

Permalink
working on separate page to view recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
savannah-nelson committed Dec 12, 2021
1 parent ee6bb97 commit f7709c9
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.example.cmsc355cookbookapp;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class RecipeRVAdapter extends RecyclerView.Adapter<RecipeRVAdapter.ViewHolder> {

// variable for our array list and context
private ArrayList<recipes_class> recipes_classArrayList;
private Context context;

// constructor
public RecipeRVAdapter(ArrayList<recipes_class> recipes_classArrayList, Context context) {
this.recipes_classArrayList = recipes_classArrayList;
this.context = context;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// on below line we are inflating our layout
// file for our recycler view items.
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recipe_rv_item, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
// on below line we are setting data
// to our views of recycler view item.
recipes_class modal = recipes_classArrayList.get(position);
holder.recipeTV.setText(modal.getRecipe());
holder.ing1TV.setText(modal.getR_ing1());
holder.ing2TV.setText(modal.getR_ing2());
holder.ing3TV.setText(modal.getR_ing2());
}

@Override
public int getItemCount() {
// returning the size of our array list
return recipes_classArrayList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

// creating variables for our text views.
private TextView recipeTV, ing1TV, ing2TV, ing3TV;

public ViewHolder(@NonNull View itemView) {
super(itemView);
// initializing our text views
recipeTV = itemView.findViewById(R.id.et_recipe);
ing1TV = itemView.findViewById(R.id.et_ing1);
ing2TV = itemView.findViewById(R.id.et_ing2);
ing3TV = itemView.findViewById(R.id.et_ing3);
}
}
}
15 changes: 15 additions & 0 deletions app/src/main/java/com/example/cmsc355cookbookapp/ViewRecipes.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.cmsc355cookbookapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

Expand All @@ -10,6 +12,8 @@ public class ViewRecipes extends AppCompatActivity {

private ArrayList<recipes_class> recipesArrayList;
private recipesDBHelper dbHelper;
private RecipeRVAdapter RecipeRVAdapter;
private RecyclerView recipesRV;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -23,6 +27,17 @@ protected void onCreate(Bundle savedInstanceState) {
//getting recipe array list from dbhelper
recipesArrayList = dbHelper.showRecipes();

//passing our array lost to our adapter class.
RecipeRVAdapter = new RecipeRVAdapter(recipesArrayList, ViewRecipes.this);
recipesRV = findViewById(R.id.idRVCourses);

// setting layout manager for our recycler view.
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ViewRecipes.this, RecyclerView.VERTICAL, false);
recipesRV.setLayoutManager(linearLayoutManager);

// setting our adapter to recycler view.
recipesRV.setAdapter(RecipeRVAdapter);




Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/example/cmsc355cookbookapp/recipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.recipes_layout);

btn_add = findViewById(R.id.btn_add);
btn_view = findViewById(R.id.btn_view);
btn_viewRecipes = findViewById(R.id.btn_view);

//initialize variables
et_recipe = findViewById(R.id.et_recipe);
Expand Down Expand Up @@ -79,7 +79,7 @@ public void onClick(View view) {
}
});

//button listener for the view button
/*button listener for the view button
btn_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand All @@ -90,6 +90,7 @@ public void onClick(View view) {
Toast.makeText(recipes.this, "Your Recipes are Amazing", Toast.LENGTH_SHORT).show();
}
});
*/

//new button listener for view button
btn_viewRecipes.setOnClickListener(new View.OnClickListener() {
Expand Down
13 changes: 10 additions & 3 deletions app/src/main/res/layout/activity_view_recipes.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewRecipes">

</androidx.constraintlayout.widget.ConstraintLayout>
<!--recycler view for displaying our courses-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/idRVCourses"
android:layout_width="match_parent"
android:layout_height="match_parent" />


</RelativeLayout>

0 comments on commit f7709c9

Please sign in to comment.