【发布时间】:2017-12-15 01:52:56
【问题描述】:
我正在尝试更新数据库中的位置。该位置以包含城市 ID 和城市名称的城市数组列表的形式存储。我正在尝试使应用程序位置感知。如果用户位置发生变化,则从 Home Frame Activity 的 onCreate() 方法调用此函数。如果您可以通过此代码并让我知道这是更新数据库的过程还是需要进行任何代码更改,那就太好了。截至目前,即使在检测到用户当前城市(与用户注册城市不同)并使用此函数更新数据库中的城市后,不同的API(如callGeneralListAPI、callConnectAPI等)仍使用旧城市id。
我附上了日志文件的截图,显示即使在检索到当前城市(加尔各答城市 id 7)后,不同的 API 仍然使用孟买的旧城市 id(城市 id 1)。
如果我在这里遗漏了一些代码逻辑,请告诉我。 帮助将不胜感激。
private void setCurrentLocation() {
Log.i("info", "SETTING location FROM home FRAME ACTIVITY- SET CURRENT
LOCATION");
Log.d("LOCATION", "LATITUDE=" + Double.toString(gpsTracker.getLatitude()));
Log.d("LOCATION", "longitude=" +
Double.toString(gpsTracker.getLongitude()));
getLocationName(gpsTracker.getLatitude(), gpsTracker.getLongitude());
location = gpsTracker.getLocation();//the bug you pointed out
Log.d("LOCATION", "LATITUDE=" + Double.toString(location.getLatitude()));
Log.d("LOCATION", "longitude=" + Double.toString(location.getLongitude()));
Log.d("CITY NAME-", addressString);
if (location != null) {
formList = AppController.getInstance().getFormList();
ArrayList<String> citieList = new ArrayList<>();
for (Map<String, String> item : formList) {
citieList.add(item.get("city"));
}
String[] citiesArray = citieList.toArray(new String[citieList.size()]);
JSONObject locationChange = new JSONObject();
try {
if (addressString != null && !TextUtils.isEmpty(addressString)) {
if (formList.indexOf(addressString) != -1) {
CITY_ID = formList.indexOf(addressString) + 1;
locationChange.put("userid",
PreferenceManager.getInstance().getUserId());
locationChange.put("location", CITY_ID);
locationChange.put("location_lat",
Double.toString(gpsTracker.getLatitude()));
locationChange.put("location_long",
Double.toString(gpsTracker.getLongitude()));
locationChange.put("connection", "0");
Log.d(TAG, "LOCATION SET INTO DATABASE-INDEX OF ADDRESS
STRING >1 " + locationChange);
this.showProgressbar();
RequestHandler.getInstance().postWithHeader(this,
getString(R.string.baseurl) + getString(R.string.settings), locationChange,
this, AppConstants.ApiRequestCodeKey.SAVE_APP_SETTINGS);
}
for (int i = 0; i < formList.size(); i++) {
if(addressString.toLowerCase().contains(formList.get(i).get("city")
.toLowerCase())
)
CITY_ID = i + 1;// the id from the json file is (i+1)
}
locationChange.put("userid",
PreferenceManager.getInstance().getUserId());
locationChange.put("location", CITY_ID);
locationChange.put("location_lat",
Double.toString(gpsTracker.getLatitude()));
locationChange.put("location_long",
Double.toString(gpsTracker.getLongitude()));
locationChange.put("connection", "0");
Log.d(TAG, "LOCATION SET INTO DATABASE- INDEX OF ADDRESS STRING
= -1 " + locationChange);
this.showProgressbar();
RequestHandler.getInstance().postWithHeader(this,
getString(R.string.baseurl) + getString(R.string.settings), locationChange,
this, AppConstants.ApiRequestCodeKey.SAVE_APP_SETTINGS
);
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
}
>
【问题讨论】:
-
此更改是否反映在服务器端?
-
这个formList是什么,它包含多少条目??
-
嗯,这就是问题所在。它不会在数据库或服务器端带来任何更改。 @Exigente05。
-
我已经回答了可能的调试情况!
-
嗯,formList 包含了用户的所有详细信息。它包含用户的姓名、用户名、电子邮件等详细信息。我不太确定条目的确切数量。