Php 填充ListView的服务中的AsyncTask
我有一段代码,我将在下面提供。它发布用户ID、纬度和经度,在php中更新当前用户lat和long,然后执行查询,检索您周围特定区域内的用户数组。(检索年龄、名称…)因此这会填充listView,但查询必须通过刷新或在打开“活动”时手动执行。但是,我有一个LocationService,其中有一个onLocationChanged(),当用户的位置更改时,它会更新。我需要在每次位置更改时执行查询/异步任务,然后在活动中填充我的listView。任何建议/代码示例 这是我的活动中调用异步任务的代码:Php 填充ListView的服务中的AsyncTask,php,android,listview,service,android-asynctask,Php,Android,Listview,Service,Android Asynctask,我有一段代码,我将在下面提供。它发布用户ID、纬度和经度,在php中更新当前用户lat和long,然后执行查询,检索您周围特定区域内的用户数组。(检索年龄、名称…)因此这会填充listView,但查询必须通过刷新或在打开“活动”时手动执行。但是,我有一个LocationService,其中有一个onLocationChanged(),当用户的位置更改时,它会更新。我需要在每次位置更改时执行查询/异步任务,然后在活动中填充我的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();
}
private void startQueryView(){
//显示进度对话框
dialog=ProgressDialog.show(这个,“,”加载…);
LocalDatabase LocalDatabase=新的LocalDatabase(此);
User User=localDatabase.getLoggedInUser();
字符串url=ServerRequests.SERVER_ADDRESS+“CombinedQuery.php”;
UserFetchData任务=新的UserFetchData(this,user.latitude,user.longitude,user.id);
task.execute(url);
}
@凌驾
公共void onFetchComplete(列表数据){
//关闭进度对话框
如果(dialog!=null)dialog.disclose();
//创建新适配器
最终用户适配器=新用户适配器(此,数据);
setAdapter(适配器);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
final int userId=adapter.getItem(position.getId();
int i;
新建下载图像(LocationSearch.this,userId+“_Profile”).execute();
对于(i=2;i<7;i++){
新建下载映像(LocationSearch.this,userId+“_Image”+Integer.toString(i)).execute();
}
ServerRequests ServerRequests=新的ServerRequests(LocationSearch.this);
serverRequests.fetchDifUserDataInBackground(userId,new GetDifUserCallback(){
@凌驾
公共作废完成(DifUser返回DifUser){
if(returnedDifUser==null){
String dir=getFilesDir().getAbsolutePath();
File File=新文件(dir,userId+“_Profile.JPG”);
布尔删除=file.delete();
如果(已删除){
Log.d(“本文件,”*************************档案已删除*************”;
}
int i;
对于(i=2;i<7;i++){
字符串dir2=getFilesDir().getAbsolutePath();
file2=新文件(dir2,userId+“_Image”+Integer.toString(i)+”.JPG”);
布尔deleted2=file2.delete();
如果(删除D2){
Log.d(“THIS”、“*******************Image”+Integer.toString(i)+“****************”;
}
}
Toast.makeText(LocationSearch.this,“出了问题!”,Toast.LENGTH\u SHORT.show();
}否则{
Intent userIdIntent=新Intent(LocationSearch.this,UserProfileOpened.class);
SharedPreferences sharedPref=getPreferences(Context.MODE\u PRIVATE);
SharedPreferences.Editor=sharedPref.edit();
editor.putInt(“userId”,userId);
commit();
putExtra(“userId”,userId);
userIDient.putExtra(“name”,returnedDifUser.name);
userIDient.putExtra(“城市”,返回difuser.city);
userIDient.putExtra(“年龄”,返回difuser.age);
userIDient.putExtra(“性别”,返回difuser.gender);
userIDinent.putExtra(“places”,returnedDifUser.places);
userIDinent.putExtra(“关系”,returnedDifUser.relationship);
userIDient.putExtra(“facebook”,returnedDifUser.facebook);
星触觉(用户属性);
}
}
});
}
});
}
@凌驾
public void onFetchFailure(字符串msg){
//关闭进度对话框
如果(dialog!=null)dialog.disclose();
//显示故障消息
Toast.makeText(this,msg,Toast.LENGTH_LONG).show();
}
如果你还需要什么,请说出来,我会编辑我的帖子。编辑:给
意图一个机会
做出一个意图
动作,如下所示:
publicstaticstringquery=“com.package.QUERY”代码>
然后在活动中扩展BroadcastReceiver
:
private class QueryReceiver extends BroadcastReceiver {
@Override
public void OnReceive(Context c, Intent i) {
startQueryView(); /*running your query when receiving an Intent*/
}
}
然后在activityOnCreate()
方法中,设置侦听器:
IntentFilter filter = new IntentFilter(QUERY);
receiver = new QueryReceiver();
registerReceiver(receiver,filter); /* registering the receiver to receive only intents with the action QUERY */
最后,在位置侦听器中,发出一个意图
:
Intent i = new Intent(QUERY);
sendBroadcast(i);
您应该查看LocationListener
的文档
使用此选项,您将知道用户的lat/long值何时发生变化 我知道当我的lat/long发生变化时,我需要的帮助与变化后发生的动作有关。这涉及在服务中运行异步任务,以便从数据库中检索的数据填充启动服务的活动中的listView。不清楚您在问什么。您是否可以修改您的问题并指出问题所在?“但是,我有一个LocationService,其中有一个onLocationChanged(),当用户的位置更改时更新。我的查询/异步任务将在每次位置更改时执行,然后在我的活动中填充我的listView。”我认为这已经足够清楚了。这个