如何将服务器的最新响应放入阵列列表中?

时间:2019-02-27 18:16:11

标签: java arraylist

当我的数据库中的联系人更新时,response可以正确显示,但arraylist却不能显示-添加联系人后arraylist可以,但是删除的是旧的他们应该离开时还在那里。你能帮我吗?

private void CheckifUserisContact() {
        StringRequest stringRequest = new StringRequest(Request.Method.POST, CHECKPHONENUMBER_URL,
            new Response.Listener<String>() {
              @Override
              public void onResponse(String response) {

                System.out.println("The contacts are :" + response);

                String MatchingContactsAsString = response.toString();
                System.out.println("The contacts are :" + MatchingContactsAsString);

                try {
                  JSONArray Object = new JSONArray(MatchingContactsAsString);
                  //for every object in the Array
                  for (int x = 0; x < Object.length(); x++) {
                    final JSONObject obj = Object.getJSONObject(x);

                    MatchingContactsAsArrayList.add(obj.getString("usernameMatch"));

                    //to stop duplicates sometimes happening....
                    HashSet<String> hashSet = new HashSet<String>();
                    hashSet.addAll(MatchingContactsAsArrayList);
                    MatchingContactsAsArrayList.clear();
                    MatchingContactsAsArrayList.addAll(hashSet);


                  }

          System.out.println("The contacts are :" + MatchingContactsAsArrayList);

第一行System.out.println("The contacts are :" + response);显示:

[{"usernameMatch":"+123456"},{"usernameMatch":"+789012"}]

这是正确的。

第二行System.out.println("The contacts are :" + MatchingContactsAsString);显示:[{"usernameMatch":"+123456"},{"usernameMatch":"+789012"}]

这是正确的。

第三行System.out.println("The contacts are :" + MatchingContactsAsArrayList);显示:[++123456, +55555, +789012]

这是错误的。我删除了+55555,但它仍在显示。

2 个答案:

答案 0 :(得分:1)

收到响应后,您可以清除其内容列表,因为它将由响应值重新填充。也无需删除重复项。

private void CheckifUserisContact() {
    StringRequest stringRequest = new StringRequest(Request.Method.POST, CHECKPHONENUMBER_URL,
        new Response.Listener<String>() {
          @Override
          public void onResponse(String response) {
            //We have a response so clear the list
            MatchingContactsAsArrayList.clear();

            String MatchingContactsAsString = response.toString();

            try {
              JSONArray Object = new JSONArray(MatchingContactsAsString);
              //for every object in the Array
              for (int x = 0; x < Object.length(); x++) {
                final JSONObject obj = Object.getJSONObject(x);
                final String userMatch = obj.getString("usernameMatch");
                if (!MatchingContactsAsArrayList.contains(userMatch) {
                    MatchingContactsAsArrayList.add(userMatch);
                }
              }

      System.out.println("The contacts are :" + MatchingContactsAsArrayList);

答案 1 :(得分:0)

尝试一下:

System.out.println("The contacts are: " + response);

String MatchingContactsAsString = response.toString();
System.out.println("The contacts are: " + MatchingContactsAsString);

try {
 JSONArray Object = new JSONArray(MatchingContactsAsString);
 //for every object in the Array
 for (int x = 0; x < Object.length(); x++) {
  final JSONObject obj = Object.getJSONObject(x);
  MatchingContactsAsArrayList.add(obj.getString("usernameMatch"));
 }

 HashSet<String> hashSet = new HashSet <String> ();
 hashSet.addAll(MatchingContactsAsArrayList);
 MatchingContactsAsArrayList.clear();
 MatchingContactsAsArrayList.addAll(hashSet);

 MatchingContactsAsArrayList.remove("+55555");

 System.out.println("The contacts are: " + MatchingContactsAsArrayList);