测试 ListView 的效率 3

继续前面的测试,

package cn.powerise.test01;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
* 测试 ListView 的效率
*
@author R9(QQ:34351088)
*/
public class List_Efficiency_3 extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

List<HashMap<String, Object>> data = null;
MyAdapter adapter = null;

data = getData();

adapter = new MyAdapter(this, data, R.layout.efficiency_item);
setListAdapter(adapter);
}

private List<HashMap<String, Object>> getData() {
List<HashMap<String, Object>> data = null;
HashMap<String, Object> item = null;

data = new ArrayList<HashMap<String, Object>>();
for (int i = 10; i < 40; i++) {
item = new HashMap<String, Object>();
item.put("image", R.drawable.icon);
item.put("id", i);
item.put("name", "Name" + i);
item.put("age", "Age" + i);
item.put("phone", "139073100" + i);
data.add(item);
}

return data;
}

static class ViewHolder {
ImageView imageView;
TextView idTextView;
TextView nameTextView;
TextView ageTextView;
TextView phoneTextView;
}

class MyAdapter extends BaseAdapter {

private List<HashMap<String, Object>> mData;
private LayoutInflater mInflater;
private int mResource;

public MyAdapter(Context context, List<HashMap<String, Object>> data, int resource) {
mData = data;
mResource = resource;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;

/**
* 测试效率(改进)
*/
if (convertView == null) {
convertView = mInflater.inflate(mResource, null);
holder = new ViewHolder();
System.out.println("-->" + position);
holder.imageView = (ImageView) convertView.findViewById(R.id.iv_image);
holder.idTextView = (TextView) convertView.findViewById(R.id.txt_id);
holder.nameTextView = (TextView) convertView.findViewById(R.id.txt_name);
holder.ageTextView = (TextView) convertView.findViewById(R.id.txt_age);
holder.phoneTextView = (TextView) convertView.findViewById(R.id.txt_phone);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

HashMap<String, Object> dataSet = mData.get(position);

holder.imageView.setImageResource(R.drawable.icon);
holder.idTextView.setText(dataSet.get("id").toString());
holder.nameTextView.setText(dataSet.get("name").toString());
holder.ageTextView.setText(dataSet.get("age").toString());
holder.phoneTextView.setText(dataSet.get("phone").toString());

return convertView;
}

@Override
public int getCount() {
return mData.size();
}

@Override
public Object getItem(int position) {
return mData.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

}
}

布局文件同前. 

本测试, 与前面不同之处在于, 添加了如下代码:

static class ViewHolder {
  ImageView imageView;
  TextView idTextView;
  TextView nameTextView;
  TextView ageTextView;
  TextView phoneTextView;
}

修改了 getView 的代码:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

  ViewHolder holder;

  /**
  * 测试效率(改进)
  */
  if (convertView == null) {
    convertView = mInflater.inflate(mResource, null);
    holder = new ViewHolder();
    System.out.println("-->" + position);
    holder.imageView = (ImageView) convertView.findViewById(R.id.iv_image);
    holder.idTextView = (TextView) convertView.findViewById(R.id.txt_id);
    holder.nameTextView = (TextView) convertView.findViewById(R.id.txt_name);
    holder.ageTextView = (TextView) convertView.findViewById(R.id.txt_age);
holder.phoneTextView = (TextView) convertView.findViewById(R.id.txt_phone);
    convertView.setTag(holder);
  } else {
    holder = (ViewHolder) convertView.getTag();
  }

  HashMap<String, Object> dataSet = mData.get(position);

  holder.imageView.setImageResource(R.drawable.icon);
  holder.idTextView.setText(dataSet.get("id").toString());
  holder.nameTextView.setText(dataSet.get("name").toString());
  holder.ageTextView.setText(dataSet.get("age").toString());
  holder.phoneTextView.setText(dataSet.get("phone").toString());

  return convertView;
}

这样做的目的是更进一步提高效率, 减少 findViewById.

 

小结: 自定义 Adapter extends BaseAdapter

1. 获取 convertView.

2. 获取 convertView 中的每个组件.

3. 手动对每个组件进行赋值.

至于什么 ListView + CheckBox, ListView + RadioButton, 也是如此, 在此就不再多述了.



你可能感兴趣的:(ListView)