【发布时间】:2015-11-14 10:09:07
【问题描述】:
我有这段代码,我将在下面提供。它发布用户 ID、纬度和经度,在 php 中更新当前用户的纬度和经度,然后执行一个查询,检索您周围某个区域内的一组用户。 (检索年龄、姓名..)所以这会填充一个 listView,但必须手动执行查询,通过刷新或打开活动。但是我有一个 LocationService,其中有一个 onLocationChanged(),当用户的位置发生变化时它会更新。每次位置更改时,我都需要执行查询/异步任务,然后在我的 Activity 中填充我的 listView。任何建议/代码示例。
这是我的活动中调用异步任务的代码:
private void startQueryView() {
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading...");
LocalDatabase localDatabase = new LocalDatabase(this);
User user = localDatabase.getLoggedInUser();
String url = ServerRequests.SERVER_ADDRESS + "CombinedQuery.php";
UserFetchData task = new UserFetchData(this, user.latitude, user.longitude, user.id);
task.execute(url);
}
@Override
public void onFetchComplete(List<UserQueryData> data) {
// dismiss the progress dialog
if (dialog != null) dialog.dismiss();
// create new adapter
final UserAdapter adapter = new UserAdapter(this, data);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final int userId = adapter.getItem(position).getId();
int i;
new DownloadImage(LocationSearch.this, userId + "_Profile").execute();
for (i = 2; i < 7; i++) {
new DownloadImage(LocationSearch.this, userId + "_Image" + Integer.toString(i)).execute();
}
ServerRequests serverRequests = new ServerRequests(LocationSearch.this);
serverRequests.fetchDifUserDataInBackground(userId, new GetDifUserCallback() {
@Override
public void done(DifUser returnedDifUser) {
if (returnedDifUser == null) {
String dir = getFilesDir().getAbsolutePath();
File file = new File(dir, userId + "_Profile.JPG");
boolean deleted = file.delete();
if (deleted) {
Log.d("THIS", "******************ProfileDeleted***********");
}
int i;
for (i = 2; i < 7; i++) {
String dir2 = getFilesDir().getAbsolutePath();
File file2 = new File(dir2, userId + "_Image" + Integer.toString(i) + ".JPG");
boolean deleted2 = file2.delete();
if (deleted2) {
Log.d("THIS", "******************Image" + Integer.toString(i) + "***********");
}
}
Toast.makeText(LocationSearch.this, "Something went wrong!", Toast.LENGTH_SHORT).show();
} else {
Intent userIdIntent = new Intent(LocationSearch.this, UserProfileOpened.class);
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("userId", userId);
editor.commit();
userIdIntent.putExtra("userId", userId);
userIdIntent.putExtra("name", returnedDifUser.name);
userIdIntent.putExtra("city", returnedDifUser.city);
userIdIntent.putExtra("age", returnedDifUser.age);
userIdIntent.putExtra("gender", returnedDifUser.gender);
userIdIntent.putExtra("places", returnedDifUser.places);
userIdIntent.putExtra("relationship", returnedDifUser.relationship);
userIdIntent.putExtra("facebook", returnedDifUser.facebook);
startActivity(userIdIntent);
}
}
});
}
});
}
@Override
public void onFetchFailure(String msg) {
// dismiss the progress dialog
if (dialog != null) dialog.dismiss();
// show failure message
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
如果您还需要什么,请说出来,我会编辑我的帖子。
【问题讨论】:
标签: php android listview service android-asynctask