簡體   English   中英

“ for”語句不會循環(聯系應用程序)

[英]'for' statement does not loop (contact application)

我試圖將我的聯系人號碼發送到服務器,以檢查這些號碼是否包含在數據庫中。 最后,應在列表視圖中顯示數據庫和電話地址列表中包含的所有號碼。

在使用for每個循環('for(String phoneNumberString:aa)')時,我得到以下警告:'for'語句不會循環...

我怎么解決這個問題?

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

在for循環的末尾有一個return語句,這意味着它將通過第一個元素並從doInBackground()調用中返回。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM