Java 从onclick响应内部传递的变量的null值
是否有任何原因使方法CreatePlan不允许我从onRecipeClicked函数(从recyclerview适配器检索)访问变量recipe_name。日志显示该值是从recyclerview检索的,但我似乎无法将其传递给其他方法。 感谢您的帮助 更新 还有一种方法可以将从createPlanRecipe创建的自动递增id分配给createPlan中的id变量吗Java 从onclick响应内部传递的变量的null值,java,android,encapsulation,Java,Android,Encapsulation,是否有任何原因使方法CreatePlan不允许我从onRecipeClicked函数(从recyclerview适配器检索)访问变量recipe_name。日志显示该值是从recyclerview检索的,但我似乎无法将其传递给其他方法。 感谢您的帮助 更新 还有一种方法可以将从createPlanRecipe创建的自动递增id分配给createPlan中的id变量吗 public class CreateMealPlan extends MainActivity { DatePicke
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);
}
}
public类CreateMealPlan扩展main活动{
日期选择器;
按钮提交;
List listRecipe=新建ArrayList();
光标c;
RecyclerView recipeList;
RecipeListAdapterRecipe;
字符串名称;
EditText editPlanName;
整数id;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.create_Dine_plan);
BottomNavigationView导航=(BottomNavigationView)findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(新的BottomNavigationView.OnNavigationItemSelectedListener(){
@凌驾
公共布尔值onNavigationItemSelected(@NonNull MenuItem item){
开关(item.getItemId()){
案例R.id.home:
意图a=新意图(CreateMealPlan.this,MainActivity.class);
星触觉(a);
打破
案例R.id.配方:
意图b=新意图(CreateMealPlan.this,RecipeSearch.class);
星触觉(b);
打破
/*案例R.id.购物清单:
意图c=新意图(CreateMealPlan.this,ShoppingList.class);
星触觉(c);
中断*/
案例R.id.mealPlan:
Intent d=新Intent(CreateMealPlan.this,MenuPlan.class);
星触觉(d);
打破
/*案例R.id.提醒:
意向e=新意向(CreateMealPlan.this,rementer.class);
星触觉(e);
中断*/
}
返回false;
}
});
datepicker=findViewById(R.id.calendarView);
ListRecipes();
RecipeListAdapter.OnRecipeClickListener侦听器=新RecipeListAdapter.OnRecipeClickListener(){
RecipeClicked上的公共void(int位置,字符串配方\名称){
Log.d(“选择配方”,配方名称);
}
};
adapterRecipe=新的RecipeListAdapter(此、listRecipe、侦听器);
recipeList=findViewById(R.id.配方);
RecyclerView.LayoutManager mLayoutManager=新的LinearLayoutManager(此,
LinearLayoutManager.VERTICAL,false);
recipeList.setLayoutManager(mLayoutManager);
recipeList.setItemAnimator(新的DefaultItemAnimator());
recipeList.setAdapter(adapterRecipe);
提交=(按钮)findViewById(R.id.create);
//在提交按钮上执行单击事件
submit.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
CreatePlan();
}
});
}
公共无效列表配方(){
listRecipe.clear();
SQLiteDatabase db=(新数据库管理器(this).getWritableDatabase());
String selectQuery=“从“+DatabaseManager.TABLE\u recipe+”中选择配方名称、图像、图像2、类别“+”;
c=db.rawQuery(selectQuery,null);
Log.d(“查询”,选择查询);
if(c.moveToFirst()){
做{
com.stu54259.plan2cook.Model.Category Category=新建com.stu54259.plan2cook.Model.Category();
category.setRecipe_name(c.getString(c.getColumnIndex(“recipe_name”));
类别.setImage(c.getInt(c.getColumnIndex(“image”));
category.setImage2(c.getString(c.getColumnIndex(“image2”));
category.setCategory_名称(c.getString(c.getColumnIndex(“category”));
添加(类别);
}而(c.moveToNext());
c、 close();
}
}
公共计划(){
editPlanName=findViewById(R.id.editPlanName);
String plan_name=editPlanName.getText().toString();
数据库管理器数据库;
int day=datepicker.getDayOfMonth();
int month=datepicker.getMonth();
int year=datepicker.getYear();
SimpleDataFormat sdf=新的SimpleDataFormat(“EEEE”);
整数d_name=天;
Log.d(“日期”,String.valueOf(d_name));
字符串dayOfWeek=sdf.format(d_名称);
字符串日期=天+“/”+月+“/”+年;
db=新数据库管理器(getApplicationContext());
db.createPlanRecipe(d_名称、星期几、配方名称);
db.createPlan(计划名称,id);
}
}
问候语!-在Create\u Plan
方法中,您正在访问全局recipe\u name
,但是click事件有歧义,因为传递的变量具有相同的确切名称,因此看起来您的全局变量没有初始化或更改。如果我没有声明全局变量,则根本不会在CreatePlan方法中标识recipe_名称。是-true。但是你有两个名字叫recipe\u name
。事件侦听器还有一个与其使用的名称相同的属性。因此,要做的第一件事是删除变量recipe\u name
,该变量正在onRecipeClicked
事件侦听器->中传递,它正在干扰全局变量。侦听器没有事件来捕获单击的值并将其分配给全局recipe\u name