Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am attempting to assign when button pressed the recipe_name selected day of week along with date picked and day of week to an SQLite table, every time a button is pressed I want the recipe_name of this table to be saved to a variable called plan_recipe1, but increasing by 1 every time until plan_recipe2, plan_recipe3 etc . These will then be saved also along side user input plan name. I cannot get my head round how to do this, this is what I have so far then I've just lost it and got confused.

What I have tried:

Java
public class CreateMealPlan extends MainActivity {

    DatePicker datepicker;
    Button submit;
    Button addrecipe;
    List<com.stu54259.plan2cook.Model.Category> listRecipe = new ArrayList<>();
    //SQLiteDatabase db;
    Cursor c;
    RecyclerView recipeList;
    RecipeListAdapter adapterRecipe;
    String recipe_name;
    EditText editPlanName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_meal_plan);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.home:
                        Intent a = new Intent(CreateMealPlan.this,MainActivity.class);
                        startActivity(a);
                        break;
                    case R.id.recipes:
                        Intent b = new Intent(CreateMealPlan.this,RecipeSearch.class);
                        startActivity(b);
                        break;
                    /*case R.id.shoppingList:
                        Intent c = new Intent(CreateMealPlan.this, ShoppingList.class);
                        startActivity(c);
                        break;*/
                    case R.id.mealPlan:
                        Intent d = new Intent(CreateMealPlan.this, MenuPlan.class);
                        startActivity(d);
                        break;
                    /*case R.id.reminder:
                        Intent e = new Intent(CreateMealPlan.this, Reminder.class);
                        startActivity(e);
                        break*/
                }
                return false;
            }
        });
        datepicker = findViewById(R.id.calendarView);
        ListRecipes();
        RecipeListAdapter.OnRecipeClickListener listener = new RecipeListAdapter.OnRecipeClickListener() {
            public void onRecipeClicked(int position, String recipe_name) {
                Log.d("Here is the data:",  recipe_name);
            }
        };
        adapterRecipe = new RecipeListAdapter(this, listRecipe, listener);
        recipeList = findViewById(R.id.recipes);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false);
        recipeList.setLayoutManager(mLayoutManager);
        recipeList.setItemAnimator(new DefaultItemAnimator());
        recipeList.setAdapter(adapterRecipe);

        submit = (Button) findViewById(R.id.create);
        addrecipe = (Button) findViewById(R.id.addRecipe);
        addrecipe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CreatePlansRecipes();


            }

        });
        // perform click event on submit button
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CreatePlan();

            }

            });
    }
    public void ListRecipes() {
        listRecipe.clear();
        SQLiteDatabase db = (new DatabaseManager(this).getWritableDatabase());
        String selectQuery = " SELECT recipe_name, image, image2, category" + " FROM " + DatabaseManager.TABLE_RECIPE + "  GROUP BY recipe_name";
        c = db.rawQuery(selectQuery, null);
        Log.d("Query", selectQuery);
        if (c.moveToFirst()) {
            do {
                com.stu54259.plan2cook.Model.Category category = new com.stu54259.plan2cook.Model.Category();
                category.setRecipe_name(c.getString(c.getColumnIndex("recipe_name")));
                category.setImage(c.getInt(c.getColumnIndex("image")));
                category.setImage2(c.getString(c.getColumnIndex("image2")));
                category.setCategory_name(c.getString(c.getColumnIndex("category")));
                listRecipe.add(category);
            } while (c.moveToNext());
            c.close();
        }

    }
    public void CreatePlansRecipes(){
        DatabaseManager db;
        int day = datepicker.getDayOfMonth();
        int month = datepicker.getMonth();
        int year = datepicker.getYear();
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
        Date d_name = new Date(day, month, year);
        String dayOfTheWeek = sdf.format(d_name);
        db = new DatabaseManager(getApplicationContext());
        db.createPlanRecipe(d_name, dayOfTheWeek, recipe_name);
        String Plan_recipe = "plan_recipe1";
        String newPlanName = "plan_recipe" + (Integer.parseInt(Plan_recipe.substring(1,Plan_recipe.length()))+1);
        newPlanName = recipe_name;


    }
    public void CreatePlan () {
        editPlanName = findViewById(R.id.editPlanName);
        String plan_name = editPlanName.getText().toString();
        DatabaseManager db;
        db = new DatabaseManager(getApplicationContext());
        db.createPlan(plan_name, newPlanName, plan_recipe2, plan_recipe3, plan_recipe4, plan_recipe5, plan_recipe6,
                plan_recipe7, plan_recipe8, plan_recipe9, plan_recipe10);


    }
}
Posted
Updated 28-Sep-20 22:06pm
v2

1 solution

Use the AUTOINCREMENT clause on the database column for the recipe entries. That way you do not need to worry about capturing the value between different invocations of the app.
 
Share this answer
 
Comments
Maciej Los 29-Sep-20 4:12am    
5ed!
Richard MacCutchan 29-Sep-20 4:16am    
:thumbsup:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900