本文介绍了如何获得多个微调查看项目的数量,并计算总价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对右左和价格实行微调列表视图(多纱厂),针对不同的产品,它们的图像。用户有选择,选择每种产品的数量(数量)。

本工作将是一个类,从BaseAdapter根据我的需求延伸。在微调的getView我设置了微调视图。

现在我想:

1)当用户在微调选择一个项目,该项目的价格计算方法为总的和右边的TextView的文本设置为总价。现在,这是运作良好,但是:当我向上滚动列表,在微调其值更改为旧的(即在位置0的值)而不是新的总和。

2)其他的事情,我想要做的就是把所有这些值都来自不同的纺纱厂在一个数组,这样在结束时,所有不同的纺纱值进一步计算总(处首先我是计算单品值,说产品的价格是选择的,他希望20件该产品,所以 50 $,并且用户totall = 20x50 )。

3)并一件事我要的是得到一个微调所选项目的数量。并以同样的方式,以保持每个喷丝的这些号码在另一个数组,使得在这些端部都被计算为所有产品总数。

下面是形象,对不起我的问题走了太久,但我真的想解决这个问题了。如果你想从我更多的事情后,请不要告诉我。

当我选择项

当我滚动屏幕纺纱所有价值和价格恢复到初始位置文本视图

这是我的code

 公共类Base_Adapter扩展了BaseAdapter
        {
            ImageView的形象;
            TextView的名称,价格;

            上下文语境;
            ArrayList的< ItemDetails> IDetails; //该项目类,有方法和字段
             RelativeLayout的R_Layout;
             活动activit;
            公共Base_Adapter(上下文的背景下,ArrayList的< ItemDetails>李)
            {
                this.context =背景;
                IDetails =力;

            }

            公共无效的setLayout(活动活动,RelativeLayout的布局){
                R_Layout =布局;
                this.activit =活动;
            }
            @覆盖
            公众诠释getCount将(){
                // TODO自动生成方法存根
                返回IDetails.size();
            }

            @覆盖
            公共对象的getItem(INT为arg0){
                // TODO自动生成方法存根
                返回null;
            }

            @覆盖
            众长getItemId(INT位置){
                // TODO自动生成方法存根
                返回0;
            }

    ////////获取视图微调////
            @覆盖
            公共查看getView(最终诠释的立场,观点简历,ViewGroup中父){
                // TODO自动生成方法存根


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

                最后ItemDetails项目= IDetails.get(位置);
                INT分钟= 1;
                INT最大=的Integer.parseInt(item.totall_Available());
                ArrayList的<字符串>的a_list =新的ArrayList<字符串>();

                的for(int i = 1; I< = MAX;我++)
                {
                     A_list.add(项目数:+ I);
                }

                 图像=(ImageView的)CV.findViewById(R.id.Item_image);
                 NAME =(TextView中)CV.findViewById(R.id.item_name);
                 价格=(TextView中)CV.findViewById(R.id.item_price);

                最后的微调量=(微调)CV.findViewById(R.id.items);
                ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(背景下,R.layout.spinner_textview,的a_list);
                quantity.setAdapter(适配器);

                 //字符串将selectedItem =(字符串)quantity.getSelectedItem();

name.setText(item.name());

                  ///项目单击/////

                quantity.setOnItemSelectedListener(新OnItemSelectedListener()
            {
                公共无效onItemSelected(适配器视图<>为arg0,查看ARG1,INT I,长ARG3)
                {

                   如果(ⅰ大于0){
     浮CAL = Float.parseFloat(item.Fisrtprise());

              浮CAL3 = CAL *我;
             price.setText(+的String.Format(%2F,CAL3).replace(,));

             字符串s = Float.toString(CAL3);
             item.Totalprice =秒;
    }
    其他{
       price.setText(+的String.Format(%2F,Float.parseFloat(item.Fisrtprise()))代替(,));
    }
                }

                公共无效onNothingSelected(适配器视图<>为arg0){

                }

            });



                返回简历;


            }
 

这是IDetails类

  @燮pressWarnings(串行)
公共类IDetails实现Serializable
{


    ContentValues​​ colmnValues​​;
    私人诠释no_of_items;
    公众持股量Totalprice;


    公共IDetails(ContentValues​​值)
    {
        colmnValues​​ =值;
    }


    公共字符串标题(){
        返回的getValue(colmnValues​​.get(标题));
    }



    公共无效setNo_of_items(INT no_of_items){
        this.no_of_items = no_of_items;
    }
    公众诠释getNo_of_items(){
        返回no_of_items;
    }


    公共无效setTotalprice(浮动Totalprice){
        this.Totalprice = Totalprice;
    }
    公众持股量getTotalprice(){
        返回Totalprice;
    }


    公共字符串IMAGEURL(){
        返回的getValue(colmnValues​​.get(IMAGEURL));
    }

    公共字符串pprice(){
        返回的getValue(colmnValues​​.get(Realprice));
    }

    公共字符串的股票(){
        返回的getValue(colmnValues​​.get(股票));
    }




    私人字符串的getValue(obj对象){
        如果(OBJ == NULL){
            返回 ;
        }
        回报(字符串)目标文件;
    }


}
 

解决方案

我刚刚更新,而无需创建新的code。首先,我会回答三个问题。

  1. 在纱厂被复位后滚动因为适配器电话 getView()方法以及它们初始化再次默认值。为了避免这种情况,并取回您的previously选定的值,你所要做的就是将它们保存在一个地图和分配回。在下面code将selectedItem 是我用来存储选定量地图每个微调。
  2. 要得到你可以用另外一个地图,用于存储总量总价每个微调的。在下面的code我用 totalPrices 吧。在 onItemSelected()我们可以把总量的具体微调。里面的onclick()方法购买按钮,您可以通过调用访问此地图 getTotalPrice()方法为适配器类。
  3. 在这个安静的容易。我们没有获得单独的阵列。我们可以得到使用将selectedItem 地图,用于存储该信息纺织所选择的值。一样的总价,就可以得到使用此地图 getTotalItems()

在你的价格计算里面 onItemSelected还()你使用的方法乘以项目价格。它应该是(I + 1)。因为i是列表中离心器的位置。它从 0

这是Base_Adapter类

 公共类Base_Adapter扩展了BaseAdapter {

    ImageView的形象;
    TextView的名称,价格;

    私人的HashMap<整数,整数GT;将selectedItem =新的HashMap<整数,整数GT;();
    私人的HashMap<整数,字符串> totalPrices =新的HashMap<整数,字符串>();

    上下文语境;
    ArrayList的< ItemDetails> IDetails; //该项目类,有方法和字段
    RelativeLayout的R_Layout;
    活动activit;


    公共Base_Adapter(上下文的背景下,ArrayList的< ItemDetails>李){
        // TODO自动生成构造函数存根
        this.context =背景;
        IDetails =力;

    }

    公共无效的setLayout(活动活动,RelativeLayout的布局){
        R_Layout =布局;
        this.activit =活动;
    }

    @覆盖
    公众诠释getCount将(){
        // TODO自动生成方法存根
        返回IDetails.size();
    }

    @覆盖
    公共对象的getItem(INT位置){
        // TODO自动生成方法存根
        返回null;
    }

    @覆盖
    众长getItemId(INT位置){
        // TODO自动生成方法存根
        返回0;
    }

    公众的HashMap<整数,整数GT; getTotalItems(){
        返回将selectedItem;
    }

    公众的HashMap<整数,字符串> getTotalPrice(){
        返回totalPrices;
    }

    ////////获取视图微调////
    @覆盖
    公共查看getView(最终诠释的立场,观点简历,ViewGroup中父){
        // TODO自动生成方法存根

        最后ItemDetails项目= IDetails.get(位置);
        INT分钟= 1;
        INT最大=的Integer.parseInt(item.stock());



        LayoutInflater充气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        查看rowView = inflater.inflate(R.layout.base_adapter,父母,假);

        ArrayList的<字符串>的a_list =新的ArrayList<字符串>();

        的for(int i = 1; I< = MAX;我++)
        {
            A_list.add(项目数:+ I);
        }


        ImageView的形象=(ImageView的)rowView.findViewById(R.id.item_image);


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

        最后的TextView价格=(TextView中)rowView.findViewById(R.id.item_price);

        微调量=(微调)rowView.findViewById(R.id.items);

        ArrayAdapter<字符串>生化的=新的ArrayAdapter<字符串>(背景下,R.layout.spinner_textview,R.id.item_list,的a_list);
        quantity.setAdapter(生化的);


        如果(selectedItem.get(位置)!= NULL){
            //这应该setAdapter后调用
            quantity.setSelection(selectedItem.get(位置));
        }

        quantity.setOnItemSelectedListener(新OnItemSelectedListener()
        {

            公共无效onItemSelected(适配器视图<>为arg0,查看ARG1,INT I,长ARG3)
            {


                如果(ⅰ大于0){
                    selectedItem.put(位置,我);

                    I = I + 1;
                    浮CAL = Float.parseFloat(item.price());

                    浮CAL3 = CAL *我;
                    price.setText(+的String.Format(%2F,CAL3).replace(,));

                    item.Totalprice = CAL3;
                }
                其他{
                    price.setText(+的String.Format(%2F,Float.parseFloat(item.price()))代替(,));

                }
                totalPrices.put(位置,price.getText()的toString());

            }

            公共无效onNothingSelected(适配器视图<>为arg0){

            }

        });

        返回rowView;

    }
}
 

所以,在你的活动课实施的onclick 方法为购买按钮。

 公共无效getTotal(查看视图){
    HashMap的<整数,整数GT; totalItems = adapter.getTotalItems();
    HashMap的<整数,字符串> totalPrices = adapter.getTotalPrice();
    的for(int i = 0; I< totalItems.size();我++){
        如果(totalItems.get(ⅰ)!= NULL){
        的System.out.println(微调否+第(i + 1)+的项目:+(totalItems.get(ⅰ)1));
        的System.out.println(微调否+第(i + 1)+总价格+ totalPrices.get(ⅰ));
        }其他{
            的System.out.println(微调否+第(i + 1)+的项目:1);
        }
    }
 

I am implementing spinner in a list view (Multiple Spinners) for different products with their images on left and prices on right. User has choice to select number (quantity) of each product.

This work is going in a Class that extends from BaseAdapter according to my needs.In getView of the spinner I set the spinner view.

Now I want:

1) when the user select an Item in a Spinner, the price of that item is calculated as total and the TextView text on the right is set to that total price. Now this is working well, but when I scroll up the list, the Spinner changes its value to the old one (i.e., the value at position 0) not the new one total.

2) The other thing i want to do is to keep all these values that came from different spinners in an array, so that at the end all different spinners values are further calculated as total (at first i was calculating the single product values, say the price of that product is 50$ and the user selected that he want 20 pieces of that product so totall=20x50 ).

3) And one other thing i want is to get the number of items selected in one spinner. And in the same way to keep these numbers of each spinner in another array, so that at the end these all are calculated as total number of all products.

Below is the image and sorry as my question gone too long, but i really want to solve this out. And if you want more things from me to post please do tell me.

When I select Items

When I scroll the screen all values in spinners and the prices in the text views reset to initial position

Here is my code

     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;


            }

And this is the IDetails Class

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


}
解决方案

I just update your code without creating new. First I will answer the three questions.

  1. The spinners are resetting after scrolled because the adapter callgetView() method and they are initializing to default value again.To avoid this and get back your previously selected values, what youhave to do is save them in a map and assign back. In followingcode "selectedItem" is the map I used to store selected amountsfor each spinner.
  2. To get total amount you can use another map which is used to storetotal price of each spinner. In the following code I used"totalPrices" for it. Inside onItemSelected() we can put totalamount for specific spinner. Inside the onclick() method of "BUY"button you can access this map by calling getTotalPrice() methodis adapter class.
  3. This is quiet easy. We don't have to get separate array. We can getthis information using "selectedItem" map which used to storeselected values of spinners. Same as the total price, you can getthis map using getTotalItems().

Also in your price calculation inside onItemSelected() method you used "i" to multiply the item price. It should be (i+1). Because "i" is the position of the list in spinner. It starts from 0.

This is the Base_Adapter class

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;

    }
}

So inside your activity class implement onclick method for the "BUY" button.

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

这篇关于如何获得多个微调查看项目的数量,并计算总价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 08:07