本文介绍了For循环运行两次结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android新手.我正在使用蓝牙创建用于printng的本机MobilePOS Android应用程序.这是我的代码:

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

有3个字符串分配给结果.当我按第二项时,我得到两次结果.当我选择第一个项目时,如项目名称数量"的格式Tea 1qty 10rs ...当我输入第二项输出时,将出现第一项+第一项+第二项,如下所示:

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 likeTea 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

我想得到结果

Tea 1qty 10rs
Coffee 1qty 12rs

像这样...请任何人都可以帮助我

like this...please any one can help me

推荐答案

一个简单的示例是,每次添加新项目时都使用一个标签并附加到该标签上.

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

代替此:

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="";

将其更改为此:

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

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

这篇关于For循环运行两次结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 15:21