如何获取多个Spinner视图中的项目数并计算总价格

时间:2014-02-11 17:41:58

标签: android android-listview android-spinner

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

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

现在我想:

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

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

3)我想要的另一件事是获取在一个微调器中选择的项目数。并且以同样的方式将每个微调器的这些数字保存在另一个数组中,以便最后这些数据都计算为所有产品的总数。

下面是图片,抱歉因为我的问题太长了,但我真的想解决这个问题。如果你想要我发布的更多信息,请告诉我。

enter image description here

当我选择项目

enter image description here

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

enter image description here

这是我的代码

     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;
    }


}

3 个答案:

答案 0 :(得分:3)

我只是在不创建新代码的情况下更新您的代码。首先,我将回答这三个问题。

  1. 由于适配器调用,旋转器在滚动后重置 getView()方法,它们再次初始化为默认值。 为了避免这种情况并取回之前选择的值,你是什么 要做的就是将它们保存在地图中并分配回去。在下面 代码“selectedItem”是我用来存储选定金额的地图 对于每个微调器。
  2. 要获得总金额,您可以使用另一张用于存储的地图 每个微调器的总价。在我使用的以下代码中 “totalPrices”为它。在onItemSelected()内,我们可以总计 特定旋转器的数量。在“BUY”的onclick()方法内 按钮,您可以通过调用getTotalPrice()方法访问此地图 是适配器类。
  3. 这很安静。我们不必获得单独的数组。我们可以得到 此信息使用“selectedItem”用于存储的地图 选定的微调器值。与总价相同,你可以得到 此地图使用getTotalItems()
  4. 同样在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”按钮实现BUY方法。

    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");
            }
        }
    

答案 1 :(得分:0)

U需要徽章(选定数量)* mrp,它将提供总金额

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

答案 2 :(得分:0)

支持自己,这个答案很长。

您已经有一个班级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
    }
}