其他分享
首页 > 其他分享> > android-如何获取多个微调器视图中的项目数并计算总价

android-如何获取多个微调器视图中的项目数并计算总价

作者:互联网

我正在为不同产品的列表视图(多个微调器)实现微调器,其图像在左侧,价格在右侧.用户可以选择每个产品的编号(数量).

根据我的需要,这项工作将在从BaseAdapter扩展的Class中进行.
在微调器的getView中,设置微调器视图.

现在我要:

1)当用户在微调框中选择一个项目时,该项目的价格计算为总计,并且右侧的TextView文本设置为该总价.现在效果很好,但是当我向上滚动列表时,微调器将其值更改为旧值(即位置0的值)而不是新的总计.

2)我想做的另一件事是将来自不同微调器的所有这些值保留在一个数组中,以便最后将所有不同微调器的值进一步计算为总计(起初,我正在计算单个乘积值,例如该产品的价格为50美元,并且用户选择了他想要20件该产品,因此totall = 20×50).

3)我想做的另一件事是在一台微调器中选择的项目数.并且以相同的方式将每个微调器的这些数字保留在另一个数组中,以便最终将所有这些计算为所有产品的总数.

下面是图片,很抱歉,我的问题回答了太久了,但是我真的很想解决这个问题.如果您想让我发更多事情,请告诉我.

当我选择项目

当我滚动屏幕时,微调器中的所有值和文本视图中的价格都重置为初始位置

这是我的代码

     public class Base_Adapter extends BaseAdapter
        {
            ImageView image;
            TextView name, price;

            Context context ;
            ArrayList<ItemDetails> IDetails; //The item class which have methods and fields
             RelativeLayout R_Layout;
             Activity activit;
            public  Base_Adapter(Context context , ArrayList<ItemDetails> li) 
            {
                this.context = context;
                IDetails = li;

            }

            public void setLayout(Activity activity, RelativeLayout layout){
                R_Layout = layout;
                this.activit = activity;
            }
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return IDetails.size();
            }

            @Override
            public Object getItem(int arg0) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return 0;
            }

    //////// Get View For Spinner////
            @Override
            public View getView(final int position, View CV, ViewGroup parent) {
                // TODO Auto-generated method stub


                LayoutInflater infleter = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                if(CV == null)
                {
                    CV = infleter.inflate(R.layout.base_adapter, null);
                }

                final ItemDetails item = IDetails.get(position);
                int min =1;
                int max = Integer.parseInt(item.totall_Available());
                ArrayList<String> A_list= new ArrayList<String>();

                for(int i=1;i<=max;i++)
                {
                     A_list.add("Number of Items :"+i);
                }

                 image = (ImageView) CV.findViewById(R.id.Item_image);
                 name = (TextView) CV.findViewById(R.id.item_name);
                 price = (TextView) CV.findViewById(R.id.item_price);

                final Spinner quantity = (Spinner) CV.findViewById(R.id.items);
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,  R.layout.spinner_textview, A_list);
                quantity.setAdapter(adapter);

                 //String selectedItem = (String) quantity.getSelectedItem();

name.setText(item.name());

                  /// ItemClick/////

                quantity.setOnItemSelectedListener(new OnItemSelectedListener()
            {
                public void onItemSelected(AdapterView<?> arg0, View arg1, int i, long arg3)
                {

                   if(i>0){
     float cal=Float.parseFloat(item.Fisrtprise());

              float cal3=cal*i;
             price.setText(""+String.format("%.2f", cal3).replace(".", ","));

             String s = Float.toString(cal3);
             item.Totalprice=s;
    }
    else{
       price.setText(""+String.format("%.2f", Float.parseFloat(item.Fisrtprise())).replace(".", ","));
    }
                }

                public void onNothingSelected(AdapterView<?> arg0){

                }

            });



                return CV;


            }

这是IDetails类

@SuppressWarnings("serial")
public class IDetails implements Serializable
{


    ContentValues colmnValues;  
    private int no_of_items;
    public float Totalprice;


    public IDetails(ContentValues values  ) 
    {
        colmnValues = values;
    }


    public String title() {
        return getValue(colmnValues.get("title"));
    }



    public void setNo_of_items(int no_of_items) {
        this.no_of_items = no_of_items;
    }
    public int getNo_of_items() {
        return no_of_items;
    }


    public void setTotalprice(float Totalprice) {
        this.Totalprice = Totalprice;
    }
    public float getTotalprice() {
        return Totalprice;
    }


    public String imageUrl() {
        return getValue(colmnValues.get("imageUrl"));
    }

    public String pprice() {
        return getValue(colmnValues.get("Realprice"));
    }

    public String stock() {
        return getValue(colmnValues.get("stock"));
    }




    private String getValue(Object obj){
        if(obj == null){
            return "";
        }
        return (String) obj;
    }


}

解决方法:

我只是更新您的代码而不创建新代码.首先,我将回答三个问题.

>滚动器滚动后将重置,因为适配器调用
getView()方法,它们将再次初始化为默认值.
为了避免这种情况并取回先前选择的值,
要做的就是将它们保存在地图中并分配回来.在下面
代码“ selectedItem”是我用来存储选定金额的地图
对于每个微调器.
>要获得总额,您可以使用另一个用于存储的地图
每个微调框的总价格.在以下代码中,我使用了
“总价格”.在onItemSelected()内部,我们可以放入总计
特定微调器的金额.在“购买”的onclick()方法内部
按钮,您可以通过调用getTotalPrice()方法访问此地图
是适配器类.
>这很安静.我们不必获取单独的数组.我们可以得到
该信息使用“ selectedItem”映射来存储
微调器的选定值.与总价一样,您可以获得
使用getTotalItems()绘制此地图.

同样,在onItemSelected()方法中的价格计算中,您使用了“ i”来乘以商品价格.应该是(i 1).因为“ i”是列表在微调器中的位置.从0开始.

这是Base_Adapter类

public class Base_Adapter extends BaseAdapter{

    ImageView image;
    TextView name, price;

    private HashMap<Integer,Integer> selectedItem=new HashMap<Integer, Integer>();
    private HashMap<Integer, String> totalPrices=new HashMap<Integer, String>();

    Context context ;
    ArrayList<ItemDetails> IDetails; //The item class which have methods and fields
    RelativeLayout R_Layout;
    Activity activit;


    public Base_Adapter(Context context , ArrayList<ItemDetails> li) {
        // TODO Auto-generated constructor stub
        this.context = context;
        IDetails = li;

    }

    public void setLayout(Activity activity, RelativeLayout layout){
        R_Layout = layout;
        this.activit = activity;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return IDetails.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public HashMap<Integer,Integer> getTotalItems(){
        return selectedItem;
    }

    public HashMap<Integer, String> getTotalPrice(){
        return totalPrices;
    }

    //////// Get View For Spinner////
    @Override
    public View getView(final int position, View CV, ViewGroup parent) {
        // TODO Auto-generated method stub

        final ItemDetails item = IDetails.get(position);
        int min =1;
        int max = Integer.parseInt(item.stock());



        LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView=inflater.inflate(R.layout.base_adapter, parent,false);

        ArrayList<String> A_list= new ArrayList<String>();

        for(int i=1;i<=max;i++)
        {
            A_list.add("Number of Items :"+i);
        }


        ImageView image=(ImageView) rowView.findViewById(R.id.item_image);


        TextView nameTextView=(TextView) rowView.findViewById(R.id.item_name);
        nameTextView.setText(item.title());

        final TextView price=(TextView) rowView.findViewById(R.id.item_price);

        Spinner quantity=(Spinner) rowView.findViewById(R.id.items);

        ArrayAdapter<String > quatity=new ArrayAdapter<String>(context, R.layout.spinner_textview, R.id.item_list, A_list);
        quantity.setAdapter(quatity);


        if(selectedItem.get(position) != null){
            //This should call after setAdapter
            quantity.setSelection(selectedItem.get(position));
        }

        quantity.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            public void onItemSelected(AdapterView<?> arg0, View arg1, int i, long arg3)
            {


                if(i>0){
                    selectedItem.put(position, i);

                    i=i+1;
                    float cal=Float.parseFloat(item.price());

                    float cal3=cal*i;
                    price.setText(""+String.format("%.2f", cal3).replace(".", ","));

                    item.Totalprice= cal3;
                }
                else{
                    price.setText(""+String.format("%.2f", Float.parseFloat(item.price())).replace(".", ","));

                }
                totalPrices.put(position, price.getText().toString());

            }

            public void onNothingSelected(AdapterView<?> arg0){

            }

        });

        return rowView;

    }
}

因此,在您的活动类内部,为“购买”按钮实现onclick方法.

public void getTotal(View view){
    HashMap<Integer, Integer>totalItems=adapter.getTotalItems();
    HashMap<Integer, String> totalPrices=adapter.getTotalPrice();
    for(int i=0;i<totalItems.size();i++){
        if(totalItems.get(i) != null){
        System.out.println("Spinner No"+(i+1)+"Items :"+(totalItems.get(i)+1));
        System.out.println("Spinner No "+(i+1)+"total price "+totalPrices.get(i));
        }else{
            System.out.println("Spinner No"+(i+1)+"Items : 1");
        }
    }

标签:android-spinner,android-listview,android
来源: https://codeday.me/bug/20191122/2057245.html