如何在 toast 中显示 arraylist 项?

how to display arraylist items in toast?

我正在使用 json 解析,我发送了五封电子邮件和五个名字,作为回应,它给出了

{
    "invitefriend": [
        {

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

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

现在我正在尝试的是我想在 toast 中获取 abc 和 def,但它会一个一个地给出

 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);
            }

你必须执行 ""+array list 然后它会在 toast 中显示所有数组列表项。或者,如果您需要一个一个地完成所有操作,则采用一个带有数组列表长度的 for 循环,因为您只需添加数组列表[i]like

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

        Toast.makeText(getContext(),arraylist[i],Toast.LENGTH_SHORT).show();
    }

这样试试。我认为这很有帮助

featured_list = new ArrayList<Featured>();
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Toast.makeText(getApplicationContext(), object.getString("thumb").toString(), Toast.LENGTH_LONG).show();
 }               

试试这个,

   JSONArray jsonary=json.getJSONArray("invitefriend");
    ArrayList<String> msgs;
            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));

                msgs = new ArrayList<String>(map.values());

                System.out.println("MESSAGE : " + msgs);

                // adding HashList to ArrayList


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

您将获得多个 Toast,因为您 Toastfor 循环中。只需将该调用移至循环外即可:

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();
        }
    });

此外,我们定义了 ArrayList msgs 来收集响应消息,我们 Toast 直接使用 msgs.toString()