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)
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
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 .
MainActivity.java , which starts 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 :
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 :
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
<?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 .
Simply replace your code with above code .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.