简体   繁体   English

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

[英]How can I put the latest response from server into my arraylist?

When my contacts are updated in my db the response is shown correctly but the arraylist is not - the arraylist is ok when contacts are added, but old ones that are deleted are still there when they should be gone. 当我的数据库中的联系人更新时, response正确显示,但arraylist未显示-添加联系人时, arraylist可以,但是应删除的旧列表仍然存在。 Can you help? 你能帮我吗?

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

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

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

This is correct. 这是对的。

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

This is correct. 这是对的。

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

This is wrong. 错了 I removed, +55555 , and yet it is still showing. 我删除了+55555 ,但仍在显示。

Once you have a response you can clear the list of its content since it will be refilled by the response values. 收到响应后,您可以清除其内容列表,因为它将由响应值重新填充。 No need to remove duplicates either. 也无需删除重复项。

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

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 Arraylist 放入 Arraylist - How can i put an Arraylist into Arraylist 如何在 arraylist 上保存从 api 收到的响应? - How can i save on arraylist the response that i receive from my api? 如何在“自定义适配器”中使sharedpreferences获得ArrayList的最新值? - How can I make sharedpreferences in Custom Adapter get the latest values of my ArrayList? 如何自动将子类放入ArrayList? - How can I automatically put subclasses in an ArrayList? 如何将行放入字符串 ArrayList? - How can I put row into a string ArrayList? 如何设置安全性,以便我的服务器只接受来自我的应用程序的请求 - How can I put in security so that my server only accepts requests coming from my app 如何停止将arraylist多次放入共享的首选项文件中? - How can I stop my arraylist being put multiple times into my shared preferences file? 如何防止ArrayList在Java GUI中重置? - How can I prevent my ArrayList from resetting in a Java GUI? 如何从ArrayList中删除Java中其他ArrayList中的对象? - how can I remove objects from my ArrayList that are also in other ArrayList in Java? 我无法将带有捆绑的ArrayList从一个片段放到另一个片段中 - I can't put a ArrayList with a bundle from a fragment into another fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM