[英]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.