我正在尝试将我的联系人号码发送到服务器,以检查我的数据库中是否包含这些号码。数据库和手机地址列表中包含的所有数字最终都应显示在列表视图中。
使用for each循环(' 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);
}
});
}}
}
答案 0 :(得分:9)
你的for循环结束时有一个return语句,这意味着它将通过第一个元素并从doInBackground()调用返回。