1

How to populate AlertDialog from Arraylist. My Arraylist something like this :

{ Brand=Weber,
  Category=Urethane,
  Code=Wpu-01,
  Description=Black,
  Quantity=5;
  Brand=Weber,
  Category=Urethane,
  Code=Wpu-02,
  Description=White,
  Quantity=10}

And I want to show that in alertdialog is something like this

Product Details

Weber Urethane Wpu-01 Black 5
Weber Urethane Wpu-02 White 10

then a button ("CLOSE")

Please help me . Thanks in advance.

This the code

public void updateJSONdata() {

    orderlist = new ArrayList<HashMap<String, String>>();


    JSONObject json = jParser.getJSONFromUrl(PRODUCTLIST_URL);


    try {


        order = json.getJSONArray(GET_PRODUCT);

        for (int i = 0; i < order.length(); i++) {

            JSONObject c = order.getJSONObject(i);

            String id = c.getString(GET_ID);

            String brand = c.getString(GET_BRAND);

            String category = c.getString(GET_CATEGORY);

            String description = c.getString(GET_DESCRIPTION);

            String code = c.getString(GET_CODE);

            String quantity = c.getString(GET_QUANTITY);

            String unit = c.getString(GET_UNIT);

            String unitprice = c.getString(GET_UNITPRICE);

            HashMap<String, String> map = new HashMap<String, String>();

            map.put(GET_ID,id);

            map.put(GET_BRAND, brand);

            map.put(GET_CATEGORY, category);

            map.put(GET_DESCRIPTION, description);

            map.put(GET_CODE, code);

            map.put(GET_QUANTITY, quantity);

            map.put(GET_UNIT, unit);

            map.put(GET_UNITPRICE, unitprice);


            orderlist.add(map);

        }

    } catch (JSONException e) {

        e.printStackTrace();

    }

}
11
  • I already used for loop but i can't get any data Commented Aug 15, 2015 at 2:34
  • int i; for(i=0;i < orderlist.toString().length();i++){ Toast.makeText(MainActivity.this,""+orderlist.get(i),Toast.LENGTH_LONG).show(); } Commented Aug 15, 2015 at 2:35
  • @Mauker orderlist is the Arraylist i used Commented Aug 15, 2015 at 2:36
  • What is the type of the ArrayList? Please show the complete code. Commented Aug 15, 2015 at 2:39
  • 1
    Do you want to show all products in one dialog? or details of a single product at a time in one dialog? Please clarify. Commented Aug 15, 2015 at 2:46

2 Answers 2

2
StringBuilder sb = new StringBuilder();
    for (HashMap<String,String> product:orderList){
        for (Map.Entry entry:product.entrySet()){
            sb.append(entry.getValue()).append(" ");
        }
    }
String msg = sb.toString();
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Product details")
                .setMessage(msg)
                .setCancelable(false)
                .setPositiveButton("CLOSE", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                }).create().show();
Sign up to request clarification or add additional context in comments.

1 Comment

Shaoib Thanks for the effort :D. I'm Glad you help me
1

To iterate through your ArrayList with HashMap and show the data on an AlertDialog, try something like:

ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();

StringBuilder sb = new StringBuilder();

for (HashMap map : list) {
    Iterator it = map.entrySet().iterator();

    while (it.hasNext()) {
        sb.append(((Map.Entry) it.next()).getValue()).append(" ");
    }
    sb.append("\n"); // Use this if you want a line break.
}

String msg = sb.toString();

new AlertDialog.Builder(this)
        .setTitle("Product details")
        .setCancelable(false)
        .setMessage(msg)
        .setPositiveButton("CLOSE", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                // Do something.
                dialogInterface.dismiss();
            }
        }).show();

This will show all the information in one AlertDialog.

If you really want to show them all in one single line, remove this line of code:

sb.append("\n"); // Use this if you want a line break.

5 Comments

what if I want to show something like this Weber Urethane Wpu-01 Black 5 Weber Urethane Wpu-02 White 10
You mean, without the line break?
Yes Please check the Products details above
I edited my post above about products details , How i can show something like the above product details please check

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.