开发者

Android/Java Beginner: Overriding ArrayAdapter's getView

开发者 https://www.devze.com 2022-12-25 23:13 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号