0

I created an android working with default listview. Now I have 2 arrays declares as below. Please help to solve this issue. Thanks!

String[] item_index = {"1","2","3","4"};
String[] item_name = {"Bacon","Egg","Cheese","Tomato" };
String[] item_price = {"$2.50","$3.00","$2.70","$2.80" };
String[] item_barcode = {"T1","T2","T3","T4"};

I want to show the resule using **for loop** as below
 1. T1-Bacon ($2.50)
 2. T2-Egg ($3.00)
 3. T3-Cheese ($2.70)
 4. T4-Tomato ($2.80)

Thanks!

8
  • 1
    Is that your all array have same size at all the time? Commented Sep 6, 2018 at 10:26
  • Thanks. Yes all my array the same size and all the time. Commented Sep 6, 2018 at 10:28
  • 1
    Ok then all the answer of given question are correct. Commented Sep 6, 2018 at 10:37
  • Yes it's correct. Commented Sep 6, 2018 at 10:56
  • Thank you. All my array size the same. One more question if I want to get this result and keep to array adapter for my listview. How to do? final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice,.....?????....) Commented Sep 6, 2018 at 11:03

4 Answers 4

1

String is immutable whereas StringBuffer and StringBuider are mutable classes. StringBuffer is thread safe and synchronized whereas StringBuilder is not, thats why StringBuilder is more faster than StringBuffer. String concat + operator internally uses StringBuffer or StringBuilder class. So it is quite good to use StringBuilder to append string.

StringBuilder s = new StringBuilder();
String[] item_index = {"1","2","3","4"};
String[] item_name = {"Bacon","Egg","Cheese","Tomato" };
String[] item_price = {"$2.50","$3.00","$2.70","$2.80" };
String[] item_barcode = {"T1","T2","T3","T4"};
String[] ansValue = new String[item_index.length];
for(int i=0;i<item_index.length;i++){
    s.setLength(0);
    s.append(item_index[i]);
    s.append(" . ");
    s.append(item_barcode[i]);
    s.append(" -");
    s.append(item_name[i]);
    s.append("(");
    s.append(item_name[i]);
    s.append(")");
    ansValue[i]=s.toString();
    System.out.println(s);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ansValue);
Sign up to request clarification or add additional context in comments.

5 Comments

Yes that answer is beautiful. I’ll try this in my project and also lets you know. Thanks!!!
Yes today I'm continuing my project. Now It's perfect. Thanks!
@KhoemVichet If its work fine then pls accept the my answer.
Yes now I'm using your code in my project. Which button lets me click or do accept your answer.
@KhoemVichet Yes You can accept as well as also upvote it.
1

A bit naive approach but will work for arrays with same size:

String[] item_index = {"1","2","3","4"};
String[] item_name = {"Bacon","Egg","Cheese","Tomato" };
String[] item_price = {"$2.50","$3.00","$2.70","$2.80" };
String[] item_barcode = {"T1","T2","T3","T4"};

public void printer() {
    for (int i = 0; i < item_index.length; i++) {
        String line = String.format("%d. %s-%s (%s)", i + 1, item_barcode[i], item_name[i], item_price[i]);
        // print or push to view
        System.out.println(line);
    }
}

If there is a chance that arrays sizes will be not the same you can try to use something like that:

public String defaultStringAtPosIfNull(String[] arr, int pos, String defaultString) {
    if (pos >= arr.length) {
        return defaultString;
    }
    return arr[pos] != null ? arr[pos] : defaultString;
}

and call it in String.format:defaultStringAtPosIfNull(item_barcode, i, "(empty)") instead calling item_barcode[i] directly.

Comments

1

A better coding approach would be having an Item class

public class Item {
    public String name;
    public String price;
    public String barcode;
    public String index;

    public Item(String name, String price, String barcode, String index) {
        this.name = name;
        this.price = price;
        this.barcode = barcode;
        this.index = index;
    }

    @Override
    public String toString() {
        return this.index + ". " + this.barcode + "-" + this.name + " (" + this.price + ") "; 
    }
}

And then just have

for(Item item : items) {
    System.out.println(item);
}

3 Comments

Thanks. Lets me this this procedure.
@KhoemVichet Upvote the answers since they re all valid and mark one as correct. We took our time to help you :)
Yes I’ll do it. Thanks for your help.
1

if all array size are same use this

for(int i=0;i<item_index.length;i++)
{
Systrem.out.println(item_index[i]+" . "+item_barcode[i]+" -"+item_name[i]+"("+item_price[i]+")");
}

Edit:

ArrayList<String> items= new ArrayList<String>();
for(int i=0;i<item_index.length;i++)
{
items.add(item_index[i]+" . "+item_barcode[i]+" -"+item_name[i]+"("+item_price[i]+")");
}
ArrayAdapter<String> itemsAdapter = 
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);

Note: Make sure that all are have same size. Otherwise you will get ArrayindexOutofBounds exception.

3 Comments

Thank you. All my array size the same. One more question if I want to get this result and keep to array adapter for my listview. How to do?
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice,.....?????....)
that case you create only list add this value in list then use array adapter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.