For loop runs twice the result?

六眼飞鱼酱① 提交于 2020-05-09 12:59:23

问题


I am new to Android. I am creating a native MobilePOS Android application for printng by using Bluetooth. This is my code:

private void showandadd() {
        /*String num="";
        num=num+String.valueOf(number);*/
        String string1="";
        String string2="";
        String string3="";


        if(num==""){
            Toast.makeText(this,"Wrong input!! try again", Toast.LENGTH_LONG).show();
        }
        if(flag==1) {
            getItem(directadd);
            if(blankflag!=1) {
                int updatedPrice = Integer.parseInt(this.price);
                int data = Integer.parseInt(this.num);
                if(data==0){
                    Toast.makeText(this,"Wrong input!! insert different quantity number", Toast.LENGTH_LONG).show();
                }else {
                    int d = updatedPrice * data;
                    this.price = d + "";

                    // String str = itemName + "      \t" + num + " No" + "      \t" + "Rs " + price;
                    printdetailsnew.add(itemName);
                    quantitynum.add(num);
                    amount.add(price);
                    num = "";
                    directadd = "";
                    flag = 0;
                    blankflag = 0;
                }
            }
            num = "";
            directadd = "";
            flag = 0;
            blankflag = 0;

        }else {
            getItem(directadd);
            if(blankflag!=1) {
                //String str = itemName + "      \t" + num + " No" + "      \t" + "Rs " + price;
                printdetailsnew.add(itemName);
                quantitynum.add("1");
                amount.add(price);
                num = "";
            }
            directadd="";
            blankflag=0;
        }
        for(int i=0;i<printdetailsnew.size();i++){
            string1=string1+ printdetailsnew.get(i)+"\t"+"\n";
            string2=string2 +"No."+ quantitynum.get(i)+"\t"+"\n";
            string3= string3 + amount.get(i)+"\n";

            result = printdetailsnew.get(i)+"\t" + "No."+ quantitynum.get(i)+"\t"+ amount.get(i)+"\n";

            System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"+result);


        }

        this.selection.setText(string1);
        this.selection2.setText(string2);
        this.selection3.setText(string3);
        num="";
    }

There are 3 strings assigned to result. I am getting result twice when I am press second item. when I am select first item getting "Itemname Quantity ampount" format like Tea 1qty 10rs...when I enter second item output will came first item+first item+second item show like this:

Tea 1qty 10rs
Tea 1qty 10rs
Coffee 1qty 12rs

I want to get result

Tea 1qty 10rs
Coffee 1qty 12rs

like this...please any one can help me


回答1:


A simple example would be to use one label and append to it each time you add a new item.

Instead of this:

for(int i=0;i<printdetailsnew.size();i++){
        string1=string1+ printdetailsnew.get(i)+"\t"+"\n";
        string2=string2 +"No."+ quantitynum.get(i)+"\t"+"\n";
        string3= string3 + amount.get(i)+"\n";

        result = printdetailsnew.get(i)+"\t" + "No."+ quantitynum.get(i)+"\t"+ amount.get(i)+"\n";

        System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"+result);


    }

    this.selection.setText(string1);
    this.selection2.setText(string2);
    this.selection3.setText(string3);
    num="";

Change it to this:

str = this.selection.getText().toString();
str += printdetailsnew.get(i)+"\t" + "No."+ quantitynum.get(i)+"\t"+ amount.get(i)+"\n";

this.selection.setText(str);
num="";


来源:https://stackoverflow.com/questions/57918473/for-loop-runs-twice-the-result

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!