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

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

概述我正在为不同产品的列表视图(多个微调器)实现微调器,其图像在左侧,价格在右侧.用户可以选择每个产品的编号(数量).根据我的需要,这项工作将在从BaseAdapter扩展的Class中进行.在微调器的getView中,设置微调器视图.现在我要:1)当用户在微调框中选择一个项目时,该项目的价格计算为

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

根据我的需要,这项工作将在从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-如何获取多个微调器视图中的项目数并计算总价全部内容,希望文章能够帮你解决android-如何获取多个微调器视图中的项目数并计算总价所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1077383.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存