2 qq863017854 QQ863017854 于 2016.03.04 11:24 提问

Android 如何在适配器中启动intent跳转activity

如题 如何在适配器中启动intent跳转activity

2个回答

Mr_dsw
Mr_dsw   Ds   Rxr 2016.03.04 11:28
已采纳

你在适配器中传入context就可以了。

 public class ProductListAdapter extends BaseAdapter {
    private Context context;
    private List<String> listData;
    private ViewHolderProduct viewHolderProduct;
    public ProductListAdapter(Context context,List<String> listData){
        this.context = context;
        this.listData = listData;
    }

    @Override
    public int getCount() {
        return listData == null ? 0 : listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData == null ? null : listData.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null || convertView.getTag() == null){
            viewHolderProduct = new ViewHolderProduct();
            convertView = LayoutInflater.from(context).inflate(R.layout.product_item_layout,null);
            viewHolderProduct.iv_pro_pic = (ImageView) convertView.findViewById(R.id.iv_imageView);
            viewHolderProduct.tv_pro_date = (TextView) convertView.findViewById(R.id.tv_pro_date);
            viewHolderProduct.tv_pro_desc = (TextView) convertView.findViewById(R.id.tv_pro_desc);
            viewHolderProduct.tv_pro_from = (TextView) convertView.findViewById(R.id.tv_pro_from);
            viewHolderProduct.tv_pro_name = (TextView) convertView.findViewById(R.id.tv_pro_name);
            viewHolderProduct.tv_pro_number = (TextView) convertView.findViewById(R.id.tv_pro_number);
            viewHolderProduct.tv_pro_price = (TextView) convertView.findViewById(R.id.tv_pro_price);
            convertView.setTag(viewHolderProduct);
        }else{
            viewHolderProduct = (ViewHolderProduct) convertView.getTag();
        }
        viewHolderProduct.tv_pro_price.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, LoginActivity.class);
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    class ViewHolderProduct{
        TextView tv_pro_number,tv_pro_name,tv_pro_desc,tv_pro_from,tv_pro_date,tv_pro_price;
        ImageView iv_pro_pic;
    }

}
lxk_1993
lxk_1993   Rxr 2016.03.04 11:29

楼上正解 吧 context 传过来就好了

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!