Greetings Fellow Programmers (in-training)!
My question pertains to the article, “Programming View Objects”, w/in the Building Android Apps coursework.
I followed along with the video in building my own contact card and have the “final” code (in so far as this exercise is concerned) for this task. See below.
private static final String CONTACT_1 = "Jay Gatsby";
private static final String CONTACT_2 = "Daisy Buchanan";
private static final String TAG = "Main Activity Tag";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View contactCard1 = findViewById(R.id.incl_cardview_contact_card_main_1);
Log.d(TAG, "Contact Card 1 is type: " + contactCard1.getClass().getName());
TextView contactName1 = contactCard1.findViewById(R.id.tv_contact_card);
contactName1.setText(CONTACT_1);
View contactCard2 = findViewById(R.id.incl_cardview_contact_card_main_2);
TextView contactName2 = contactCard2.findViewById(R.id.tv_contact_card);
contactName2.setText(CONTACT_2);
}
I’d like to better understand the relationship b/t the View object, contactCard1, and the TextView object, contactName1.
First, why is FindViewById() first called w/o any object (1), then, it is called on an instance(2)?
(1)
View contactCard1 = findViewById(R.id.incl_cardview_contact_card_main_1);
(2)
TextView contactName1 = contactCard1.findViewById(R.id.tv_contact_card);
It is to say, why do we need to access the contactCard1 View object in order to access the contactName1 TextView object?
I hope I’ve crafted this question clearly!
Mike