如何获取多个 Spinner View 中的商品数量并计算总价

Posted

技术标签:

【中文标题】如何获取多个 Spinner View 中的商品数量并计算总价【英文标题】:How to get the number of Items in multiple Spinner View and calculate the total price 【发布时间】:2014-03-09 16:01:34 【问题描述】:

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

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

现在我想要:

1) 当用户在 Spinner 中选择一个项目时,该项目的价格计算为总价,右侧的 TextView 文本设置为该总价。现在这运行良好,但是当我向上滚动列表时,Spinner 将其值更改为旧值(即位置 0 处的值)而不是新值。

2) 我想做的另一件事是将来自不同微调器的所有这些值保留在一个数组中,以便最后将所有不同的微调器值进一步计算为总计(在首先我在计算单个产品的价值,假设该产品的价格是 50 美元,用户选择他想要 20 件该产品,所以 totall=20x50)。

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;
    

        

【问题讨论】:

Spinners 正在重置,因为当您选择新数量时,您不会将选择保存在 ItemDetails 类中(其中您确实有一个项目数量字段(在getView() 方法你有ItemDetails 和下面你有IDetails,它们是一样的吗?!))。保存 ItemDetails 对象中的项目数后,您可以在 getView 方法中使用该值并将Spinner 上的选择设置为该值(使用setSelection(/*proper value*/) 抱歉,构造函数名称输入错误。 这看起来更像是一个编程练习,而不是一个编程问题。你被困在哪里了? 您需要在数据bean(Datalist 对象)中保留用户选择的微调器位置和 textview 计算值。因此,当您从数据列表中向上滚动检查旧值并进行相应更新时。 【参考方案1】:

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

    由于适配器调用,微调器在滚动后正在重置 getView() 方法,它们再次初始化为默认值。 为了避免这种情况并取回您之前选择的值,您 所要做的就是将它们保存在地图中并分配回来。在以下 代码“selectedItem”是我用来存储选定金额的地图 每个微调器。 要获得总金额,您可以使用另一个用于存储的地图 每个微调器的总价格。在我使用的以下代码中 “totalPrices”为它。在onItemSelected()里面我们可以放总 特定微调器的数量。 "BUY" 的 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;

    

因此,在您的活动类中为“BUY”按钮实现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_guy【参考方案2】:

你需要徽章(选定数量)*将给出总金额的mrp

供参考 https://developer.android.com/training/contacts-provider/display-contact-badge.html

【讨论】:

【参考方案3】:

振作起来,这个答案很长。

您已经有一个课程IDetails,用于描述待售物品。我会建议一种稍微不同的方法,但如果您不喜欢它,请跳至适配器部分。但是,我将在整个答案中使用我更改后的模型,因为它们使您的某些问题更容易解决。

模型

创建一个描述项目的基类。我们会将这个类抽象化并对其进行扩展,以描述您销售的各个商品。

public abstract class ShoppingItem 

    /* You can add more fields here, for example an image */

    protected String title;
    protected String description;

    protected int cost;

    // Each object keeps track of how many of itself is being bought
    protected int numberBought = 1;

    public String getTitle() 
        return title;
    
    public void setTitle(String title) 
        this.title = title;
    

    public String getDescription() 
        return description;
    
    public void setDescription(String description) 
        this.description = description;
    

    public int getCost() 
        return cost;
    
    public void setCost(int cost) 
        this.cost = cost;
    

    public int getNumberBought() 
        return numberBought;
    

    public void setNumberBought(int numberBought) 
        this.numberBought = numberBought;
       

现在可以轻松创建单个项目:

public class Banana extends ShoppingItem 
    public Banana() 
        this.title = "Banana";
        this.description = "A tasty banana";
        this.cost = 100;
    
   

public class Apple extends ShoppingItem 
    public Apple() 
        this.title = "Apple";
        this.description = "A fresh apple";
        this.cost = 200;
    

适配器

既然我们已经整理好了模型,我们如何以最有效的方式用它们填充ListView?使用自定义 Adapter,就像您所做的那样。我们将修复滚动时重置的微调器,我们还将添加一些出色的性能增强功能。

public class ShoppingAdapter extends ArrayAdapter<ShoppingItem> 

    // We use this LayoutInflater to inflate the xml for each row in the list
    private LayoutInflater mInflater;

    // This is the strings that will populate the spinners
    // Can be moved to res/values/string-array.xml to prevent hardcoded strings
    private String[] spinnerArray = new String[]  "No. items: 1", 
                                                   "No. items: 2",
                                                   "No. items: 3",
                                                   "No. items: 4";

    public ShoppingAdapter(Context context, int resource, ArrayList<ShoppingItem> objects) 
        super(context, 0, objects);

        // Initialize the declared LayoutInflater 
        mInflater = LayoutInflater.from(context);
    

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 

        // This is a simple reference holding class, so we don't have to call
        // findViewById() every time getView() is called. Improved performance :)
        ViewHolder holder = null;

        if(convertView == null)  
            // If convertView is null, then we don't have a view to place
            // our data in. We have to inflate one ourself
            convertView = mInflater.inflate(R.layout.list_item, null);

            // Store a reference to the Spinner and the TextView in 
            // the ViewHolder object
            holder = new ViewHolder();          
            holder.spinner = (Spinner) convertView.findViewById(R.id.spinner);
            holder.textView = (TextView) convertView.findViewById(R.id.total);

            // Attach the ViewHolder object to convertView, as a tag
            convertView.setTag(holder);
        
        else 
            // If convertView is non-null then we can fetch the ViewHolder object
            // we stored in the tag earlier. We now have references to both the Spinner 
            // and TextView, through the ViewHolder object.
            holder = (ViewHolder) convertView.getTag();
        

        // If the holder is non-null, we are good to go!
        if(holder != null) 
            // Fetch the item from the underlying ArrayList
            final ShoppingItem item = getItem(position);

            // We need this to be final, so we can access it inside the 
            // onItemSelected()-callback
            final TextView textview = holder.textView;

            // Populate the Spinner
            holder.spinner.setAdapter(new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, spinnerArray));

            // Add a callback when an item is selected in the Spinner
            holder.spinner.setOnItemSelectedListener(new OnItemSelectedListener() 

                @Override
                public void onItemSelected(AdapterView<?> adapterView, View v, int pos, long id) 
                    // Spinner position is 0-based, so we have to increment 
                    // by one
                    item.setNumberBought(pos + 1);  

                    // Update the total amount in the TextView
                    textview.setText(Integer.toString(item.getCost() * item.getNumberBought()));
                

                @Override
                public void onNothingSelected(AdapterView<?> arg0)  // We don't care
            );

            // Set the spinner to the correct position
            // By asking the underlying item how many is bought,
            // the spinner will always show the correct number.
            holder.spinner.setSelection(getItem(position).getNumberBought()-1);
           

        return convertView;
    

    // This guys job is to hold references to the 
    // views we need to access in each row
    private static class ViewHolder 
        public Spinner spinner;
        public TextView textView;
    

如何使用它

public class MainActivity extends ListActivity 

    private ShoppingAdapter mAdapter;
    private ArrayList<ShoppingItem> items;

    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // If savedInstanceState is null, then we have a "fresh" start.
        // Create the 
        if(savedInstanceState == null) 
            items = new ArrayList<ShoppingItem>();
            items.add(new Banana());
            items.add(new Apple());
            items.add(new Banana());
            items.add(new Apple());
            items.add(new Banana());
            items.add(new Apple());
            items.add(new Banana());
        
        // If savedInstanceState is non-null, we should be able to fetch 
        // the list of items
        else 
            items = (ArrayList<ShoppingItem>) savedInstanceState.getSerializable("items");
        

        mAdapter = new ShoppingAdapter(this, 0, items);

        getListView().setAdapter(mAdapter);
    

    @Override
    protected void onSaveInstanceState(Bundle outState) 
        super.onSaveInstanceState(outState);

        // We have to keep our ListView persistent through screen rotations.
        // Lets store the list of items in the "savedInstanceState" Bundle.
        outState.putSerializable("items", items);
    

轻松获取所需的所有元数据,例如总成本、产品总数等。

@Override
public void onClick(View v) 
    int totalCost = 0;
    int numBananas = 0;
    int numApples = 0;

    int totalNum = mItems.size();

    for(ShoppingItem item : mItems) 
        int numberBought = item.getNumberBought();

        totalCost += item.getCost() * numberBought;

        if(item instanceof Banana) 
            numBananas += numberBought;
        
        else if(item instanceof Apple) 
            numApples += numberBought;
        
        // etc, etc
    

【讨论】:

以上是关于如何获取多个 Spinner View 中的商品数量并计算总价的主要内容,如果未能解决你的问题,请参考以下文章

POJ-2516(最小费用最大流+MCMF算法)

怎么实现Android的Spinner控件不可编辑

android spinner怎么用

Python!Are you kidding me?

Android Spinner - 如何使下拉视图透明?

View(视图)——AutoCompleteTextView Spinner和消息提示