android适配器的简单使用
-
private ListView listView;// 列表组件
public static ArrayList<NutritionListViewBean> list = new ArrayList<NutritionListViewBean>();// 数据源
private NutritionAdapter adapter;// 适配器
-
// 创建适配器对象
adapter = new NutritionAdapter(this);
// 设定数据源
loadData();
// 设置数据源
adapter.setList(list);
// 将适配器设置到ListView中
listView.setAdapter(adapter);
-
public class NutritionAdapter extends BaseAdapter {
// 设置组视图的数据源
private ArrayList<NutritionListViewBean> list = new ArrayList<NutritionListViewBean>();
// 设置数据源
public void setList(ArrayList<NutritionListViewBean> list) {
this.list = list;
}
// 加载布局
private LayoutInflater mInflater;
public NutritionAdapter(Context context) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int arg0) {
return list.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
final GroupViewHolder holder;
if (null == convertView) {
convertView = mInflater.inflate(R.layout.listview_nutrition, null);
holder = new GroupViewHolder();
holder.listNutritionImageView = (com.yongting.view.CircleImage) convertView.findViewById(R.id.listNutritionImageView);
convertView.setTag(holder);
} else {
holder = (GroupViewHolder) convertView.getTag();
}
// 从List中得到一个Bean
NutritionListViewBean bean = list.get(position);
// 点击分享
holder.listNutritionShareImageView.setTag(position);
holder.listNutritionShareImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
// 职称
holder.listNutritionProfessionalRankTextView.setText(bean.getProfessional_rank());
}
class GroupViewHolder {
com.yongting.view.CircleImage listNutritionImageView;
TextViewlistNutritionProfessionalRankTextView;
}
}