public class MyAdapter extends BaseAdapter {
private Context context;
public List<Students> list;
private LayoutInflater mLayoutInflater;
public MyAdapter (Context context, List<Students> list) {
this.context = context;
this.list= list;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
if (convertView == null) {
holder = new ViewHolder();
convertView= mLayoutInflater.inflate(R.layout.students_item,null);
holder.name=(TextView)convertView.findViewById(R.id.tv_name);
holder.money = (TextView)convertView.findViewById(R.id.tv_money);
holder.time= (TextView)convertView.findViewById(R.id.tv_time);
//设置控件集到convertView
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
Students students = list.get(position);
//更新布局内容
String name = students .getName();
String amount = students .getMoney();
String time = students .getDate();
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
SimpleDateFormat sdf2 = new SimpleDateFormat( "MM-dd" );
try {
Date date = sdf.parse( time );
String time2 = sdf2.format(date);
holder.time.setText(time2);
holder.name.setText(name);
holder.money.setText(amount);
} catch (ParseException e) {
e.printStackTrace();
}
return convertView;
}
private static class ViewHolder {
TextView name;
TextView money;
TextView time;
}