简体   繁体   中英

Want to print full Array.asList , Only printing one line

I am developing an Application in Android Studio to that prints how many of each item you can buy with the given amount of currency. It printed flawlessly when run as a Java program in Eclipse but I can not get it to print more than one line in the TextView Box.

I've noticed it will pick the most Expensive item you can afford 1 of and print it alone, leading me to believe it runs through the list and only prints the last one that passes as affordable. I've read about needing to use a StringBuilder and such but have found little information on how to convert my Array.asList over to this. Here is my code.

gCalc.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText diamondInput = (EditText) findViewById(R.id.diamondInput);

            try {
                int diamonds = Integer.parseInt(diamondInput.getText().toString());
                List<Gifts> gift = Arrays.asList(new Gifts[]{new Gifts("Gold star", 10), new Gifts("Love Bear", 10), new Gifts("Lillies", 10), new Gifts("Box Of Chocolate", 20), new Gifts("Taco", 20), new Gifts("Thumbs Up", 30), new Gifts("Panda", 40), new Gifts("Beer", 40), new Gifts("Patriot", 52), new Gifts("Eagle", 52), new Gifts("Gold Chain", 80), new Gifts("Roses", 100), new Gifts("Champagne", 100), new Gifts("Snow", 100), new Gifts("Candy", 100), new Gifts("Kiss", 200), new Gifts("Candy Hearts", 250), new Gifts("Peach", 300), new Gifts("EggPlant", 300), new Gifts("Fireworks", 500), new Gifts("GemDrop", 600), new Gifts("Crown", 600), new Gifts("Cupcakes", 700), new Gifts("Heart Balloon", 800), new Gifts("Sports Car", 1000), new Gifts("Smoke Rings", 1000), new Gifts("purple Diamond", 2500), new Gifts("Cupid", 5000), new Gifts("Gold Watch", 5000), new Gifts("Castle", 5000), new Gifts("Yacht", 10000), new Gifts("Jet", 20000)});


                double coins = (double) diamonds / 2.5D;
                Iterator var5 = gift.iterator();

                while(var5.hasNext()) {
                    Gifts Gifts = (Gifts)var5.next();
                    int qty = (int)Gifts.getQty(coins);
                        if(qty > 0) {
                        result.setText("You can buy " + qty + " " + Gifts.name);
                                    }
                }

            }
            catch (Exception e) {
                // friendly error to the user: field is incorrect
            }

I need it to print as this EX:

You can buy X amount of Y You can buy X amount of Y You can buy X amount of Y :end Printing every item that can be bought and it's quantity.

If you look into Android API documentation ( https://developer.android.com/reference/android/widget/TextView.html ) , this is what it states:

void setText (CharSequence text) - sets the text to be displayed. - this means that each time you call this method, new text overrides the old one.

It seems that you are looking for append() method:

void append(CharSequence text) - convenience method to append the specified text to the TextView's display buffer, upgrading it to EDITABLE if it was not already editable. append() method doesn't override previously set text.

Another way to append text to the previously stored in TextView is to combine these methods:

result.setText(result.getText() + "text that you want to append to the previous one")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM