android适配器的简单使用
1.
private ListView listView;// 列表组件
public static ArrayList<NutritionListViewBean> list = new ArrayList<NutritionListViewBean>();// 数据源
private NutritionAdapter adapter;// 适配器
public static ArrayList<NutritionListViewBean> list = new ArrayList<NutritionListViewBean>();// 数据源
private NutritionAdapter adapter;// 适配器
2.
// 创建适配器对象
adapter = new NutritionAdapter(this);
// 设定数据源
loadData();
// 设置数据源
adapter.setList(list);
// 将适配器设置到ListView中
listView.setAdapter(adapter);
adapter = new NutritionAdapter(this);
// 设定数据源
loadData();
// 设置数据源
adapter.setList(list);
// 将适配器设置到ListView中
listView.setAdapter(adapter);
3.
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);
// 设置组视图的数据源
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) {
}
});
} 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());
holder.listNutritionProfessionalRankTextView.setText(bean.getProfessional_rank());
return convertView;
}
class GroupViewHolder {
com.yongting.view.CircleImage listNutritionImageView;
TextView listNutritionProfessionalRankTextView;
com.yongting.view.CircleImage listNutritionImageView;
TextView listNutritionProfessionalRankTextView;
}
}