I am new to Android. I found this code in my book that i am learning from. Its not explained properly I created a Drink java class and then i used drinks[]in my activity. On click it passes the Id of the clicked Array to another activity using intent. Now in that other activity i want to display the name of the drink the image and the description that we stored in the array I understood that we have stored the array Id in the Drinks class object but why did we use the getter methods to get the name and description and the image. They are not linked with the array? They have no code inside them? Then what is actually happening
can someone explain?
Drinks.java class code
Class Drinks
{
private String name ;
private String description ;
private int rid ;
public static final Drinks[] drinks = {
new Drinks("Latte", "A couple of espresso shots with steamed milk",
R.drawable.latte),
new Drinks("Cappuccino", "Espresso, hot milk, and a steamed milk foam",
R.drawable.cappuccino),
new Drinks("Filter", "Highest quality beans roasted and brewed fresh",
R.drawable.filter)
};
public Drinks(String name, String description, int rid) {
this.name = name;
this.description = description;
this.rid = rid;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public int getRid() {
return rid;
}
}
The activity code where i am using the drinks[] and passing its Id
listDrinks = findViewById(R.id.listDrinks);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1 ,Drinks.drinks);
listDrinks.setAdapter(adapter);
listDrinks.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Main2Activity.this , Main3Activity.class);
intent.putExtra("id",id);
startActivity(intent);
}
});
-------------------------------------------------------
The other activity where i am passing the array id and using getter methods to set name description and image to display
textView = findViewById(R.id.textname);
text_description = findViewById(R.id.text_description);
imageView = findViewById(R.id.photo);
Intent intent= getIntent();
int drinkId = Integer.parseInt(intent.getStringExtra("id"));
Drinks d = Drinks.drinks[drinkId];
textView.setText(d.getName());
text_description.setText(d.getDescription());
imageView.setImageResource(d.getRid())
;
What I have tried:
i cant understand the concept as the constructor is setting entirely different values.