0

I am trying to send the numbers of my contacts to the server to check if these numbers are contained in my database. All numbers contained in database and in the address list of the phone finally should be displayed in a listview.

While using the for each loop ('for (String phoneNumberString : aa)') I get this warning: 'for' statement does not loop...

How can I solve this problem?

    public class AllUserActivity extends ListActivity {

// Progress Dialog
private ProgressDialog pDialog;

// Creating JSON Parser object
JSONParser jParser = new JSONParser();

ArrayList<HashMap<String, String>> usersList;

// url to get all users list
private static String url_all_user = "http://.../.../.../get_user.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_USER = "user";
private static final String TAG_PHONE = "phone";
private static final String TAG_UID = "uid";
private static final String TAG_USERNAME = "username";

String phoneNumber;

ArrayList<String> aa= new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.all_user);

    // Hashmap for ListView
    usersList = new ArrayList<HashMap<String, String>>();

    // Loading users in Background Thread
    new LoadAllUsers().execute();

    getNumber(this.getContentResolver());

}

// get all numbers of contacts
public void getNumber(ContentResolver cr)
{
    Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
    // use the cursor to access the contacts
    while (phones.moveToNext())
    {
        phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        String phoneNumberString = phoneNumber.replaceAll("\\D+","");

        // get phone number
        System.out.println(".................."+phoneNumberString);
        aa.add(phoneNumberString);
    }
    phones.close();// close cursor
}

class LoadAllUsers extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(AllUserActivity.this);
        pDialog.setMessage("Loading users. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected String doInBackground(String... args) {

        ArrayList<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();

            for (String phoneNumberString : aa){
            params.add(new BasicNameValuePair("phone[]", phoneNumberString));

            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_all_user, "GET", params);

            // Check your log cat for JSON response
            Log.d("All users: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    JSONArray users = json.getJSONArray(TAG_USER);

                    // looping through all contacts
                    for (int i = 0; i < users.length(); i++) {
                        JSONObject c = users.getJSONObject(i);

                        // Storing each json item in variable
                        String uid = c.getString(TAG_UID);
                        String phone = c.getString(TAG_PHONE);
                        String username = c.getString(TAG_USERNAME);

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_UID, uid);
                        map.put(TAG_PHONE, phone);
                        map.put(TAG_USERNAME, username);

                        // adding HashList to ArrayList
                        usersList.add(map);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
        return null;
    }

    // After completing background task Dismiss the progress dialog

    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all users
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {

                 // Updating parsed JSON data into ListView
                ListAdapter adapter = new SimpleAdapter(
                        AllUserActivity.this, usersList,
                        R.layout.list_users, new String[] { TAG_PHONE,
                        TAG_USERNAME},
                        new int[] { R.id.phone, R.id.name });
                // updating listview
                setListAdapter(adapter);
            }
        });
    }}
  }
Timitrov
  • 211
  • 1
  • 3
  • 14

1 Answers1

9

You have a return statement at the end of your for loop, meaning it will go through the first element and return from your doInBackground() call.

tachyonflux
  • 20,103
  • 7
  • 48
  • 67
  • Thanks, the warning disappeared, but I don't know why my listview is still empty... According to the Logcat there is no JSON response. What's the problem? – Timitrov Dec 07 '14 at 20:37