android ListView中给Item的每个控件设置单击事件

最近做了一个android项目,在每一个ListView的Item中还有两个RelativeLayout控件,需要对这两个RelativeLayout控件设置单击事件,解决如下:
只贴关键代码了

//ListView变量
private ListView lv_pcorder = null;
//需要传递的变量
List list1 = new ArrayList();
List list2 = new ArrayList();
Map pics = Util.getAnecdoteSeven();
Map picid = new HashMap();

//配置BasicAdapter
lv_pcorder = (ListView) findViewById(R.id.lv_pcorder);
lv_pcorder.setAdapter(new PicCenterListApdapter(PicCenterList.this, list1, list2, pics, picid));

//BaseAdapter实现类
public class PicCenterListApdapter extends BaseAdapter {
 
  private Context context;
   private List list1;
   private List list2;
   private Map map;
   private Map picid;

   public PicCenterListApdapter(Context context, List list1, List list2, Map map, Map picid) {
      this.context = context;
      this.list1 = list1;
      this.list2 = list2;
      this.map = map;
      this.picid = picid;
   }

   @Override
   public int getCount() {
      return list1.size();
   }

   @Override
   public Object getItem(int arg0) {
      return null;
   }

   @Override
   public long getItemId(int position) {
      return 0;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
      ViewHolder vh = null;
      if (convertView == null) {
         vh = new ViewHolder();
         LayoutInflater inflater = LayoutInflater.from(context);
         convertView = inflater.inflate(R.layout.pcorderlist, null);
         vh.lin0 = (RelativeLayout) convertView.findViewById(R.id.lin0);
         
         vh.lin0.setOnClickListener(new PCount(position)); //设置单击事件
         vh.lin2 = (RelativeLayout) convertView.findViewById(R.id.lin2); //设置单击事件
         vh.lin2.setOnClickListener(new COrder(position)); //设置单击事件
         vh.lin3 = (RelativeLayout) convertView.findViewById(R.id.lin3);
         vh.lin3.setOnClickListener(new COrder(position));
         vh.timeitem = (TextView) convertView.findViewById(R.id.timeitem);
         vh.timeitem.setText(list1.get(position));
         vh.countitem = (TextView) convertView.findViewById(R.id.countitem);
         vh.countitem.setText(list2.get(position));
         if (map.containsKey(list1.get(position))) {
            vh.smallpic = (ImageView) convertView.findViewById(R.id.smallpic);
            vh.smallpic.setImageResource(picid.get(map.get(list1.get(position)).getAddress()));
            vh.title = (TextView) convertView.findViewById(R.id.title);
            vh.title.setText(map.get(list1.get(position)).getTitle());
            vh.picintro = (TextView) convertView.findViewById(R.id.picintro);
            vh.picintro.setText("     "+Util.getString(map.get(list1.get(position)).getContent())+"....");
         }
         
         convertView.setTag(vh);
      } else {
         vh = (ViewHolder) convertView.getTag();
      }
      return convertView;
   }
   
  //单击事件实现
   class COrder implements OnClickListener {
      public int position;
      public COrder(int p) {
         position = p;
      }
      @Override
      public void onClick(View v) {
         Intent intent = new Intent();
         intent.setClass(context, CountOrder.class);
         Bundle b = new Bundle();
         b.putString("datatime", list1.get(position));
         intent.putExtras(b);
         context.startActivity(intent);
      }
      
   }
  
  //单击事件实现  
   class PCount implements OnClickListener {
      public int position;
      public PCount(int p) {
         position = p;
      }
      public void onClick(View v) {
         Intent intent = new Intent();
         intent.setClass(context, PicCenterItem.class);
         Bundle b = new Bundle();
         b.putString("datatime", list1.get(position));
         intent.putExtras(b);
         context.startActivity(intent);
      }
   }

   public final class ViewHolder {
      public RelativeLayout lin0;
      public RelativeLayout lin2;
      public RelativeLayout lin3;
      public TextView timeitem;
      public TextView countitem;
      public ImageView smallpic;
      public TextView title;
      public TextView picintro;
   }

}

转载于:https://my.oschina.net/u/1013545/blog/180845

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值