[英]sharing variables among methods of same class in android
My Problem is I can't use the food1(Object of Food class which Stores the value of food which is selected) object's values in onclickfoodimage() function which I have initialized in onCreate() (onClickFoodImage is the method which is called when image is clicked) 我的问题是我无法在我在onCreate()中初始化的onclickfoodimage()函数中使用food1(存储所选食物值的Food类的Object)对象的值(onClickFoodImage是图像处理时调用的方法被点击)
FoodActivity Class 美食活动课
public class FoodActivity extends Activity {
public static final String EXTRA_MESSAGE2="message";
Food food1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent=getIntent();
int Foodno=intent.getExtras().getInt(EXTRA_MESSAGE2);
food1=Food.food[Foodno];
ImageView image=(ImageView)findViewById(R.id.imageView3);
image.setImageResource(food1.getFoodImageId());
}
public final void onclickfoodimage()
{
TextView textview=(TextView)findViewById(R.id.textView4);
textview.setText(food1.getFoodName());
TextView textView=(TextView)findViewById(R.id.textView5);
textView.setText(food1.getFoodDescription());
}
}
Food Class containing the food Data of which food1 is object 包含食物的食物类,其中food1是对象的数据
public class Food {
String foodName,foodDescription;
int foodImageId;
static Food food[]={new Food("Dosa","A South Indian Dish",R.drawable.dosa),new Food("Paneer","A Famous And Delicious Dish made from Paneer",R.drawable.paneer)};
Food(String name,String description,int id)
{
this.foodName=name;
this.foodDescription=description;
foodImageId=id;
}
String getFoodName()
{
return foodName;
}
String getFoodDescription()
{
return foodDescription;
}
int getFoodImageId()
{
return foodImageId;
}
public String toString()
{
return foodName;
}
}
It seems onclickfoodimage() is not being called when you click on ImageView , Here I have develop a complete program for you , I have not tested this , hope this will work for you . 当您单击ImageView时似乎没有调用onclickfoodimage() ,这里我为您开发了一个完整的程序,我没有测试过,希望它对您有用 。
MainActivity.java , which starts FoodActivity.java MainActivity.java ,它启动FoodActivity.java
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE2="message";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this , FoodActivity.class).putExtra(EXTRA_MESSAGE2,1);
startActivity(intent);
}
} }
FoodActivity.java : FoodActivity.java:
public class FoodActivity extends Activity {
public static final String EXTRA_MESSAGE2="message";
Food food1;
static ArrayList<Food> foodArray = new ArrayList<>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
foodArray.add(new Food("Dosa","A South Indian Dish",R.drawable.dosa)) ;
foodArray.add(new Food("Paneer","A Famous And Delicious Dish made from Paneer",R.drawable.paneer)) ;
Intent intent=getIntent();
int Foodno=intent.getExtras().getInt(EXTRA_MESSAGE2);
food1=foodArray.get(Foodno);
ImageView image=(ImageView)findViewById(R.id.imageView3);
image.setImageResource(food1.getFoodImageId());
}
public final void onclickfoodimage(View v)
{
TextView textview4 =(TextView)findViewById(R.id.textView4);
textview4.setText(food1.getFoodName());
TextView textView5 = (TextView)findViewById(R.id.textView5);
textView5.setText(food1.getFoodDescription());
}
}
Food.java : Food.java:
public class Food {
String foodName,foodDescription;
int foodImageId;
Food(String name,String description,int id)
{
this.foodName=name;
this.foodDescription=description;
foodImageId=id;
}
String getFoodName()
{
return foodName;
}
String getFoodDescription()
{
return foodDescription;
}
int getFoodImageId()
{
return foodImageId;
}
public String toString()
{
return foodName;
}
}
activity_main.xml activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.shishupalshakya.accessstringvariable.MainActivity">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<TextView
android:id="@+id/textView5"
android:layout_below="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<ImageView
android:id="@+id/imageView3"
android:layout_below="@+id/textView5"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher"
android:onClick="onclickfoodimage"
/>
</RelativeLayout>
Note : Don't forgot to register FoodActivity.java in the AndroidManifest.xml file . 注意:不要忘记在AndroidManifest.xml文件中注册FoodActivity.java。
Simply replace your code with above code . 只需用上述代码替换您的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.