列表视图中的项目总数和子项目计数

时间:2017-03-15 05:08:19

标签: android listview toolbar

我正在开发购物车应用。我想计算listview中所选项目和子项目的总数,以及想要在工具栏中显示总项目。我不知道如何实现这个目标。我完成了一些代码,但是当我点击列表视图中的不同项目时,总计数从0开始。

please see image

这是我的代码。

 @Override
public View getView(final int position, View convertView, ViewGroup parent) {
  final  Holder  holder=new Holder();
    View rowView;
    itemCount_database = new ItemCount_database(acontext);

    mypref = acontext.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
    editor = mypref.edit();

    rowView = inflater.inflate(R.layout.productlist_listview_single_row, null);
    holder.productname =(TextView) rowView.findViewById(R.id.productlist_singlerow_product_title);
    holder.product_unit =(TextView) rowView.findViewById(R.id.productlist_singlerow_product_unit);
    holder.product_discount =(TextView) rowView.findViewById(R.id.productlist_singlerow_product_discount);
    holder.product_previous_price =(TextView) rowView.findViewById(R.id.productlist_singlerow_previous_price);
    holder.product_latest_price =(TextView) rowView.findViewById(R.id.productlist_singlerow_latest_price);
    holder.product_count=(TextView)rowView.findViewById(R.id.product_count);
    holder.product_img=(ImageView) rowView.findViewById(R.id.productlist_singlerow_imgevew);
    holder.btn_add=(ImageView)rowView.findViewById(R.id.product_list_singlerow_btn_add);
    holder.btn_remove=(ImageView)rowView.findViewById(R.id.product_list_singlerow_btn_remove);
    holder.layout_product_discount =(LinearLayout)rowView.findViewById(R.id.layout_product_discount);

    holder.productname.setText(aproductname[position]);
    holder.product_unit.setText(aproduct_unit[position]);
    holder.product_discount.setText(aproduct_discount[position]);
    holder.product_previous_price.setText(aproduct_previous_price[position]);
    holder.product_latest_price.setText(aproduct_latest_price[position]);
    holder.product_img.setImageResource(aproduct_img[position]);
    holder.product_previous_price.setPaintFlags(holder.product_previous_price.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

    holder.btn_add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             curent_position = getItemId(position);

            if(curent_position == pervius_position)
            {
                add_curent_item_count +=1;
                curent_product_count =Integer.toString(add_curent_item_count);
                holder.product_count.setText(curent_product_count);
                totalunit +=1;

            }
            else
            {
                add_curent_item_count =0;
                //totalunit -=1;
            }

            pervius_position=curent_position ;
            total_product_count =Integer.toString(totalunit);








        //   if(holder.product_count.equals("0")) { holder.product_count.setText(""); }
            if(listener!=null) {listener.onAddClick(totalunit);}
            editor.commit();
        }
    });

    holder.btn_remove.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            curent_position = getItemId(position);

                        if(curent_position == pervius_position)
                        {
                            if(add_curent_item_count ==0)
                            {
                                Toast.makeText(acontext, "no item in cart ", Toast.LENGTH_SHORT).show();
                                holder.product_count.setText("");
                            }
                            else
                            {
                                 totalunit -=1;
                                 add_curent_item_count -= 1;
                                 curent_product_count = Integer.toString(add_curent_item_count);
                                 holder.product_count.setText(curent_product_count);
                            }
                        }
                        else
                        {

                        }

                        pervius_position=curent_position ;

                        if(listener!=null) {listener.onAddClick(totalunit);}







        }
    });

    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(acontext, "You Clicked "+aproductname[position], Toast.LENGTH_LONG).show();
        }
    });



    if(acoupencode[position]==0)
    {
       holder.layout_product_discount.setVisibility(View.GONE);
    }
    else
    {
        holder.layout_product_discount.setVisibility(View.VISIBLE);
    }



    holder.product_img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent myint =new Intent(acontext.getApplicationContext(), Product_view.class);
            acontext.startActivity(myint);
        }
    });

    holder.productname.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent myint =new Intent(acontext.getApplicationContext(), Product_view.class);
            acontext.startActivity(myint);
        }
    });



    return rowView;
}

2 个答案:

答案 0 :(得分:0)

试试这个,

将aproduct_count数组初始化为零

    holder.btn_add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             int add_curent_item_count = aproduct_count[position];
             add_curent_item_count=add_curent_item_count+1;
             holder.product_count.setText(add_curent_item_count);
             aproduct_count[position] = add_curent_item_count;
    });


    holder.btn_remove.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
             int remove_curent_item_count = aproduct_count[position];
             remove_curent_item_count=remove_curent_item_count-1;
             holder.product_count.setText(remove_curent_item_count);
             aproduct_count[position] = remove_curent_item_count;
        }
    });

答案 1 :(得分:-1)

当你可以点击添加项目增加两者的值时,你可以将静态公共变量用于静态

public class Constant{

public static int mainCount = 0;
public static int subCount = 0;

}

像这样增加

Constant.mainCount++;

Constant.mainCount = Constant.mainCount+1;