Reputation: 149
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();
}
}
Upvotes: 1
Views: 691
Reputation: 11497
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.
Upvotes: 1
Reputation: 2095
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();
Upvotes: 2