【问题标题】:AsyncTask Inside A Service Populating A ListView填充 ListView 的服务中的 AsyncTask
【发布时间】: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


    【解决方案1】:

    编辑:试一试Intents。

    像这样进行Intent 操作:

    public static String QUERY = "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*/
      }
    }
    

    然后在你的活动OnCreate()方法中,设置一个监听器:

    IntentFilter filter = new IntentFilter(QUERY);
    receiver = new QueryReceiver();
    registerReceiver(receiver,filter); /* registering the receiver to receive only intents with the action QUERY */
    

    最后,在位置监听器中,发射Intent

    Intent i = new Intent(QUERY);
    sendBroadcast(i);
    

    您应该查看LocationListener 的文档。

    http://developer.android.com/reference/android/location/LocationListener.html

    使用此功能,您将知道用户的经纬度何时发生变化!

    【讨论】:

    • 我知道当我的纬度/经度发生变化时,我需要帮助的事情与它发生变化后发生的动作有关。这涉及在服务中运行异步任务,以便从数据库中检索到的数据填充到启动服务的 Activity 中的 listView。
    • 不清楚你在问什么。您能否修改您的问题并指出问题所在?
    • "但是我有一个 LocationService,并且在其中,它有一个 onLocationChanged(),它会在用户位置更改时更新。我的查询/异步任务在每次位置更改时执行,然后填充我的我的活动中的列表视图。”我认为这已经足够清楚了。我提供的代码在我的活动中,但我希望它在我的服务中 onLocationChanged() 以便每次用户位置更改时都会执行查询。然后它将同时用新数据填充我的 listView。
    • 抱歉给您带来了困惑,我更新了我的答案,可以在不移动代码的情况下解决您的问题。
    • 是的!这正是我所需要的,工作完美,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 2013-08-12
    相关资源
    最近更新 更多