从onclick响应内部传递的变量的空值

问题描述

是否有任何原因导致方法CreatePlan不允许我从从recyclerview适配器检索的onRecipeClicked函数访问变量recipe_name。日志显示该值是从recyclerview中检索到的,但是我似乎无法将其传递给另一种方法。 任何帮助表示赞赏。

更新 还有一种方法可以将从createPlanRecipe创建的自动递增的id分配给createPlan中的id变量?

public class CreateMealPlan extends MainActivity {

    DatePicker datepicker;
    Button submit;
    List<com.stu54259.plan2cook.Model.Category> listRecipe = new ArrayList<>();
    Cursor c;
    RecyclerView recipeList;
    RecipeListAdapter adapterRecipe;
    String recipe_name;
    EditText editPlanName;
    Integer id;
    @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("Recipe selected",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);

        // 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 CreatePlan(){
        editPlanName = findViewById(R.id.editPlanName);
        String plan_name = editPlanName.getText().toString();
        DatabaseManager db;
        int day = datepicker.getDayOfMonth();
        int month = datepicker.getMonth();
        int year = datepicker.getYear();
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
        Integer d_name = day;
        Log.d("Date",String.valueOf(d_name));
        String dayOfTheWeek = sdf.format(d_name);
        String date = day + "/" + month + "/" +year;
        db = new DatabaseManager(getApplicationContext());
        db.createPlanRecipe(d_name,dayOfTheWeek,recipe_name);
        db.createPlan(plan_name,id);

    }


}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...