用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

购物车

2020-11-03 作者:云代码会员举报

[java]代码库

package com.addaegg.androidwork4.adapter;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.addaegg.androidwork4.R;
import com.addaegg.androidwork4.activity.MainActivity;
import com.addaegg.androidwork4.activity.ProInfoActivity;
import com.addaegg.androidwork4.activity.StoreActivity;
import com.addaegg.androidwork4.entity.CartProductInfo;
import com.addaegg.androidwork4.entity.ProductInfo;

/**
 * 2列ListView的适配器
 * @author tongleer.com
 *
 */
public class MyAdapter extends BaseAdapter{
    protected Context context;
    protected LayoutInflater inflater;
    protected int resource;
    protected ArrayList<CartProductInfo> list;
    public MyAdapter(Context context, int resource, ArrayList<CartProductInfo> list){
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.resource = resource;
        if(list==null){
            this.list=new ArrayList<CartProductInfo>();
        }else{
            this.list = list;
        }
    }
    @Override
    public int getCount() {
        if(list.size()%2>0) {
            return list.size()/2+1;
        } else {
            return list.size()/2;
        }
    }
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null ) {
            convertView = inflater.inflate(resource, null);
            vh = new ViewHolder();
            vh.list_1=(LinearLayout) convertView.findViewById(R.id.list_1);
            vh.list_2=(LinearLayout) convertView.findViewById(R.id.list_2);
            vh.item1_img=(ImageView) convertView.findViewById(R.id.item1_img);
            vh.item1_info=(TextView) convertView.findViewById(R.id.item1_info);
            vh.item1_price=(TextView) convertView.findViewById(R.id.item1_price);
            vh.item1_num=(TextView) convertView.findViewById(R.id.item1_num);
            vh.item2_img=(ImageView) convertView.findViewById(R.id.item2_img);
            vh.item2_info=(TextView) convertView.findViewById(R.id.item2_info);
            vh.item2_price=(TextView) convertView.findViewById(R.id.item2_price);
            vh.item2_num=(TextView) convertView.findViewById(R.id.item2_num);
            convertView.setTag(vh);
        }else {
            vh = (ViewHolder)convertView.getTag();
        }
        int distance =  list.size() - position*2;
        int cellCount = distance >= 2? 2:distance;
        final List<CartProductInfo> itemList = list.subList(position*2,position*2+cellCount);
        if (itemList.size() >0) {
            vh.item1_img.setImageResource(itemList.get(0).getImg());
            vh.item1_info.setText(itemList.get(0).getInfo());
            Log.e("getPeopleNum","¥"+itemList.get(0).getOnePrice()+"券后价");
            vh.item1_price.setText("¥"+itemList.get(0).getOnePrice()+"券后价");
            vh.item1_num.setText(itemList.get(0).getPeopleNum()+"人付款");
            vh.list_1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent intent= new Intent(context, ProInfoActivity.class);
                    CartProductInfo cartProductInfo = new CartProductInfo(
                            itemList.get(0).getImg(),
                            itemList.get(0).getName(),
                            itemList.get(0).getInfo(),
                            itemList.get(0).getNum(),
                            itemList.get(0).getOnePrice(),
                            itemList.get(0).getAllPrice(),
                            itemList.get(0).getPeopleNum());
                    intent.putExtra("product",cartProductInfo);
                    context.startActivity(intent);
                }
            });
            if (itemList.size() >1){
                vh.list_2.setVisibility(View.VISIBLE);
                vh.item2_img.setImageResource(itemList.get(1).getImg());
                vh.item2_info.setText(itemList.get(1).getInfo());
                vh.item2_price.setText("¥"+itemList.get(1).getOnePrice()+"券后价");
                vh.item2_num.setText(itemList.get(1).getPeopleNum()+"人付款");
                vh.list_2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent= new Intent(context, ProInfoActivity.class);
                        CartProductInfo cartProductInfo = new CartProductInfo(
                                itemList.get(1).getImg(),
                                itemList.get(1).getName(),
                                itemList.get(1).getInfo(),
                                itemList.get(1).getNum(),
                                itemList.get(1).getOnePrice(),
                                itemList.get(1).getAllPrice(),
                                itemList.get(1).getPeopleNum());
                        intent.putExtra("product",cartProductInfo);
                        context.startActivity(intent);
                    }
                });
            }else{
                vh.list_2.setVisibility(View.INVISIBLE);
            }
        }
        return convertView;
    }
    /**
     * 封装ListView中item控件以优化ListView
     * @author tongleer
     *
     */
    public static class ViewHolder{
        ImageView item1_img,item2_img;
        TextView item1_info,item1_price,item1_num,item2_info,item2_price,item2_num;
        LinearLayout list_1,list_2;
        //TextView tv2;
    }
}


分享到:
更多

网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。