ListView BaseAdapter

自定义Adapter:

public View getView(int position, View convertView, ViewGroup parent) {
  ViewHolder viewHolder;
  if(convertView==null){
   convertView=LayoutInflater.from(context).inflate(R.layout.menu_item, null);
   viewHolder=new ViewHolder();
   viewHolder.img=(ImageView) convertView.findViewById(R.id.iv_menuImg);
   viewHolder.name=(TextView) convertView.findViewById(R.id.tv_menuName);
   viewHolder.price=(TextView) convertView.findViewById(R.id.tv_menuPrice);
   convertView.setTag(viewHolder);
  }
  viewHolder=(ViewHolder) convertView.getTag();
  viewHolder.img.setImageResource(arrayList.get(position).getMenuImg());
  viewHolder.name.setText(arrayList.get(position).getMenuName());
  viewHolder.price.setText(arrayList.get(position).getPrice()+"元");
  return convertView;
 }
 class ViewHolder{
  ImageView img;
  TextView name;
  TextView price;
 }

 

listView绑定Adapter:

initList(arrayList);
  
  listView1=(ListView) findViewById(R.id.listView1);
  
  MyAdapter myAdapter=new MyAdapter(TestAct4.this, arrayList);
  listView1.setAdapter(myAdapter);
  
  listView1.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView parent, View view, int position, long id) {
    setTitle(arrayList.get(position).getMenuName()+"  单价:"
   +arrayList.get(position).getPrice()+"元");
    
   }
   
   
  });

 

 

你可能感兴趣的:(自写:)