question about listview from APIDemos example

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

question about listview from APIDemos example

Postby zxrxy » Wed Nov 25, 2009 3:22 am

In APIDemos, class:com.example.android.apis.view.list14
public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;

// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text, null);

// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);

convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}

// Bind the data efficiently with the holder.
holder.text.setText(DATA[position]);
holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);

return convertView;
}

static class ViewHolder {
TextView text;
ImageView icon;
}
This adpater uses ViewHoder, it said
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.

if I change code :
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text, null);

// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);

// convertView.setTag(holder);

// Bind the data efficiently with the holder.
holder.text.setText(DATA[position]);
holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);

}
// else {
// // Get the ViewHolder back to get fast access to the TextView
// // and the ImageView.
// holder = (ViewHolder) convertView.getTag();
// }



return convertView;
Is the change right?
Need I use ViewHolder?
When I creat the contentview, I bind the data.
Why it does't bind data while creating view?
zxrxy
Freshman
Freshman
 
Posts: 2
Joined: Wed Nov 25, 2009 3:10 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests