how to display arraylist items in toast?

albert

I am using json parsing,i am sending five emails and five names and in response it gives

{
    "invitefriend": [
        {

            "message": "Friend Request sent to : abc"
        },
        {

            "message": "Friend Request sent to : def"
        }
    ]
}

now what i am trying is i want to get abc and def in toast,but it gives one by one seperatly

 JSONArray jsonary=json.getJSONArray("invitefriend");

            for (int i = 0; i < jsonary.length(); i++) {
                JSONObject c = jsonary.getJSONObject(i);
                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();
                // adding each child node to HashMap key => value
                // map.put(INTERESTACCEPT_USER_ID, c.getString(INTERESTACCEPT_USER_ID));
                map.put(TAG_SUCCESS_INVITE,c.getString(TAG_SUCCESS_INVITE));
                map.put(TAG_SUCCESS_MESSAGE, c.getString(TAG_SUCCESS_MESSAGE));

                ArrayList<String> msgs=new ArrayList<String>();
                msgs=map.put(TAG_SUCCESS_MESSAGE, c.getString(TAG_SUCCESS_MESSAGE));
                System.out.println("MESSAGE : " + msg);

                // adding HashList to ArrayList

               getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getActivity(),""+msgs, Toast.LENGTH_LONG).show();
                    }
                });
                data.add(map);
            }
Mike M.

You're getting multiple Toasts because you're Toasting inside the for loop. Simply move that call to outside the loop:

JSONArray jsonary=json.getJSONArray("invitefriend");
ArrayList<String> msgs=new ArrayList<String>();
String msg = null;

for (int i = 0; i < jsonary.length(); i++) {
    JSONObject c = jsonary.getJSONObject(i);
    // creating new HashMap
    HashMap<String, String> map = new HashMap<String, String>();
    // adding each child node to HashMap key => value
    // map.put(INTERESTACCEPT_USER_ID, c.getString(INTERESTACCEPT_USER_ID));
    map.put(TAG_SUCCESS_INVITE,c.getString(TAG_SUCCESS_INVITE));
    map.put(TAG_SUCCESS_MESSAGE, c.getString(TAG_SUCCESS_MESSAGE));

    msg=map.put(TAG_SUCCESS_MESSAGE, c.getString(TAG_SUCCESS_MESSAGE));
    msgs.add(msg);
    System.out.println("MESSAGE : " + msg);

    // adding HashList to ArrayList
    data.add(map);
}

getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getActivity(), msgs.toString(), Toast.LENGTH_LONG).show();
        }
    });

Also, we've defined the ArrayList msgs to collect the response messages, which we Toast directly with msgs.toString().

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to display arraylist items in toast?

From Dev

How to get all items in arraylist and display it in a listview

From Java

How to display Toast in Android?

From Dev

How to display toast in AsyncTask

From Dev

How to display newline / CR in toast()?

From Dev

How to display Toast from AlertDialog?

From Dev

How to display new Toast that overrides old Toast in no time in android

From Dev

How to display new Toast that overrides old Toast in no time in android

From Dev

How to display the toast meassage only if checkbox is checked?

From Dev

How to get android Toast Height before display

From Dev

How to Display Emoticons/Emoji in Snackbar or Toast / Textview

From Dev

How to display Toast Message if TextView is changed?

From Dev

How to display the toast meassage only if checkbox is checked?

From Dev

How to display Toast notification popup as modal dialog?

From Dev

How to Display the computed perimeter using Toast

From Dev

How to center toast vertically and display at bottom of screen

From Dev

How do I display a Toast in a setOnClickListener?

From Dev

How to display an ArrayList in a RecyclerView?

From Dev

How to display an arraylist of objects?

From Dev

How to stop adding items to the arraylist?

From Dev

How to display arraylist in gridview in android

From Dev

How to display arraylist of files in ListView

From Java

How do you display a Toast using Kotlin on Android?

From Dev

How to display a Toast message in the top of the page in my android application

From Dev

How to display Toast from a Service after main Activity finishes?

From Dev

How to overcome lag in display in showing Toast every second

From Dev

How to display "Working" toast from Google Docs add-on

From Dev

How to display "Working" toast from Google Docs add-on

From Dev

How to display Alert/Toast While my Response is NULL

Related Related

  1. 1

    how to display arraylist items in toast?

  2. 2

    How to get all items in arraylist and display it in a listview

  3. 3

    How to display Toast in Android?

  4. 4

    How to display toast in AsyncTask

  5. 5

    How to display newline / CR in toast()?

  6. 6

    How to display Toast from AlertDialog?

  7. 7

    How to display new Toast that overrides old Toast in no time in android

  8. 8

    How to display new Toast that overrides old Toast in no time in android

  9. 9

    How to display the toast meassage only if checkbox is checked?

  10. 10

    How to get android Toast Height before display

  11. 11

    How to Display Emoticons/Emoji in Snackbar or Toast / Textview

  12. 12

    How to display Toast Message if TextView is changed?

  13. 13

    How to display the toast meassage only if checkbox is checked?

  14. 14

    How to display Toast notification popup as modal dialog?

  15. 15

    How to Display the computed perimeter using Toast

  16. 16

    How to center toast vertically and display at bottom of screen

  17. 17

    How do I display a Toast in a setOnClickListener?

  18. 18

    How to display an ArrayList in a RecyclerView?

  19. 19

    How to display an arraylist of objects?

  20. 20

    How to stop adding items to the arraylist?

  21. 21

    How to display arraylist in gridview in android

  22. 22

    How to display arraylist of files in ListView

  23. 23

    How do you display a Toast using Kotlin on Android?

  24. 24

    How to display a Toast message in the top of the page in my android application

  25. 25

    How to display Toast from a Service after main Activity finishes?

  26. 26

    How to overcome lag in display in showing Toast every second

  27. 27

    How to display "Working" toast from Google Docs add-on

  28. 28

    How to display "Working" toast from Google Docs add-on

  29. 29

    How to display Alert/Toast While my Response is NULL

HotTag

Archive