Android/Java Beginner: Overriding ArrayAdapter's getView
Firstly I am new to android and Java so this is a beginners question.
I have some code that overrides the ArrayAdapter's getView method. Here is the code
public View getView(int position, View convertView, ViewGroup parent) {
TextView label = (TextView)convertView;
if (convertView == null) {
convertView = new TextView(ctxt);
label = (TextView)convertView;
}
label.setText(items[position]);
return (convertView);
}
My question is: why d开发者_Go百科oes label.setText(items[position]);
affect the convertView
return value? How are they related / linked?
TextView label = (TextView)convertView;
doesn't set label to be a copy of convertView
,
it is convertView
. It's a reference to the same object. So when you do
label.setText(items[position]);
, it does it on convertView.
Looking at your code convertView
and label
are two variables that both reference the same TextView
object. Whatever you do with either variable will be reflected in the TextView
object they reference.
TextView label = (TextView)convertView;
that both reference the same object(reference of label =reference of convertView),so ,convertView will be reflected by the label object.
精彩评论