Android 笔记二

在ListActivity中

ListAdapter adapter = new SimpleCursorAdapter(
                 
this, // Context.
                 android
.R.layout.two_line_list_item,  // Specify the row template to use (here, two columns bound to the two retrieved cursor rows).
                 mCursor
,                                              // Pass in the cursor to bind to.
                 
new String[] {People.NAME, People.COMPANY},           // Array of cursor columns to bind to.
                 
new int[] {android.R.id.text1, android.R.id.text2});
android.R.layout.two_line_list_item这是每一行的样式 ,之所能一行代码 就把每一行的样式添加进去是因为在SimpleCusrorAdapter中的getView()方法中通过LayoutInflater把每一行都

添加了一次inflater.inflate(R.layout.row,parent,false);还有一种方法就是再创建一个类型,而这个类型已经包括了要显示的类型,而且在创建这个view的时候已经用过LayoutInflater

ListActivity中支持ArrayAdapter 和SimpleCursorAdapter这两种类型,所以如果有什么特殊的定制(如每行显示的不一样,也要继承这两个类型来处理)。

但是上面的方法中性能都不好,adapter的机制:在getView(int Position,View convertView,ViewGroup parent)中,第一次加载时,并不是把所有的项都加载进来,而只是加载所能显示出来的

项,在滚动过中,再去加载新的项,而在每一次加载中convertView为null,这时候需要创建我们自己的项,而后来就不为空,android是把创建一些,然后循环使用,以增加性能,所以我们自己订制

时候,就可以判断convertView是否为Null如果是,而创建如果不是,则直接使用convertView。

性能最佳实践:


View Code
class WallPer{
private View _veiw=null;
private TextView label=null;
private ImageView icon=null;
public WallPer(View view)
{
this._veiw=view;
}
public TextView getLabel()
{
if(label==null){
_veiw.findViewById(R.id.xxxxx);
}
return label;
}
public ImageView getIcon()
{
if(icon==null){
_veiw.findViewById(R.id.xxx);
}
return icon;
}
}
public View getView(int position, View convertView, ViewGroup parent) {

WallPer helper
=null;
if(convertView==null){
LayoutInflater inflater
=(LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
convertView
=inflater.inflate(R.layout.activity_list_item, parent);
helper
=new WallPer(convertView);
convertView.setTag(helper);
}
else{
helper
=(WallPer) convertView.getTag();
helper.getIcon();
}
}

posted @ 2011-07-06 21:57  IT圈儿  阅读(240)  评论(0)    收藏  举报