赞
踩
1.创建xml布局: listview的Item要显示的内容(封装bean)
2.创建ItemBean方法,初始化xml中要显示的控件
3.获取要显示的数据格式化为List
4.创建Adapter继承BaseAdapter,完成所有方法
5.在类的初始化函数中声明List并与获取的数据元关联
6.在类的初始化函数中使用传入的context对象对LayoutInflater进行初始化
private List<ListInfo> mlist;
private LayoutInflater mInflater;//xml布局填充器
public ListViewAdapter(Context context,List<ListInfo> list){
mlist =list;
mInflater = LayoutInflater.from(context);//通过传入的context对象初始化mInflater
}
7.新建ViewHolder类,初始化所有控件Item
public static class ViewHolder {
TextView tv_title;
TextView tv_des;
}
8.在GetView 方法里用初始化的mInflater加载Item布局对象赋给convertview管理
//使用convertview管理填充布局
convertView = mInflater.inflate(R.layout.online_listinfo_lay, null);
9.通过判断covertview对象内是否为空,进行优化
为空则使用findviewById查找出所有控件赋给holder.item
holder.tv_des = (TextView) convertView.findViewById(R.id.tv_des);
并为view设置标签
convertView.setTag(holder);
不为空则使用gettag获取缓存值:
holder = (ViewHolder) convertView.getTag();
10.从list中取出值赋给holder.item
ListInfo info = mlist.get(position);
holder.tv_des.setText(info.jarDescript);
holder.tv_title.setText(info.jarName);
照虎画猫:
package com.beini.listhelper;
import java.util.List;
import com.beini.uiautom.R;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
public class ListViewAdapter extends BaseAdapter {
private List<ListInfo> mlist;
private LayoutInflater mInflater = null;// xml布局填充器
private String mTag;
public ListViewAdapter(Context context, List<ListInfo> list, String tag) {
mlist = list;
mInflater = LayoutInflater.from(context);// 通过传入的context对象初始化mInflater
mTag = tag;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mlist.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mlist.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
if (mTag.equals("Online")) {
// 使用convertview填充布局
convertView = mInflater.inflate(R.layout.online_listinfo_lay,
null);
} else {
// 使用convertview填充布局
convertView = mInflater.inflate(R.layout.local_listinfo_lay,
null);
}
holder = new ViewHolder();
holder.tv_des = (TextView) convertView.findViewById(R.id.tv_des);
holder.tv_title = (TextView) convertView
.findViewById(R.id.tv_title);
// 为view设置标签
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// EditText ed_sx = (EditText) view.findViewById(R.id.ed_sx);
// EditText ed_cs = (EditText) view.findViewById(R.id.ed_cs);
ListInfo info = mlist.get(position);
//区分online和local数据,避免空指针
if (!TextUtils.isEmpty(info.jarDescript)){
holder.tv_des.setText(info.jarDescript);}
holder.tv_title.setText(info.jarName);
return convertView;
}
public static class ViewHolder {
TextView tv_title;
TextView tv_des;
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。