【问题标题】:Android display gets rendered before getting values from backendAndroid 显示在从后端获取值之前被渲染
【发布时间】:2020-04-21 08:26:03
【问题描述】:

我对 android 有点陌生,并尝试使用 parse 作为后端开发自己的应用程序。在这里,我试图从解析中获取帖子的 cmets 数量。 当我使用 asynctask 进行回调时,即使在数据返回之前,UI 中的数据也会被渲染,即使它们不是,我的所有 cmets 也显示为零。当我调试时,我得到了正确的大小,但它甚至在此之前就显示出来了。 当我尝试使用如下代码所示的日志进行调试时,这就是我得到调试的方式。

2020-04-21 13:53:58.984 22308-22589/com.project I/gg: outside
2020-04-21 13:53:59.090 22308-22595/com.project I/gg: outside
2020-04-21 13:53:59.127 22308-22598/com.project I/gg: outside
2020-04-21 13:53:59.162 22308-22601/com.project I/gg: outside
2020-04-21 13:53:59.203 22308-22589/com.project I/gg: outside
2020-04-21 13:53:59.238 22308-22595/com.project I/gg: outside
2020-04-21 13:53:59.247 22308-22308/com.project I/gg: post
2020-04-21 13:53:59.470 22308-22308/com.project I/gg: post
2020-04-21 13:53:59.472 22308-22308/com.project I/gg: post
2020-04-21 13:53:59.523 22308-22308/com.project I/gg: inside
2020-04-21 13:53:59.843 22308-22308/com.project I/gg: inside
2020-04-21 13:54:00.156 22308-22308/com.project I/gg: inside
2020-04-21 13:54:00.176 22308-22308/com.project I/gg: inside
2020-04-21 13:54:00.325 22308-22308/com.project I/gg: inside

如您所见,该过程没有按正确的顺序进行。

如果有人能指出错误,我会很高兴。 如果需要任何其他资源来回答这个问题,请随时询问我会添加它。 谢谢。

 static public class CommentAsyncTask extends AsyncTask<Void, String, String> {
            String objectId;
            TextView commentsView;

        CommentAsyncTask(String objectId, TextView commentsView) {
            this.objectId = objectId;
            this.commentsView = commentsView;
        }
        @Override
        protected String doInBackground(Void... voids) {
            ParseQuery<ParseObject> objectParseQuery = ParseQuery.getQuery("Comments");
            objectParseQuery.whereEqualTo("quoteId", objectId);
            objectParseQuery.setLimit(20);
            objectParseQuery.orderByAscending("createdAt");
            final Bitmap[] tempBitmap = new Bitmap[1];
            final int[] size = new int[1];
            Log.i("gg", "outside");
            objectParseQuery.findInBackground(new FindCallback<ParseObject>() {
                @Override
                public void done(List<ParseObject> objects, ParseException e) {
                    if (e == null) {
                        size[0] = objects.size();
                        Log.i("gg", "inside");
                    }
                }
            });
            return String.valueOf(size[0]);
        }


        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if(s != null){
                commentsView.setText("View all " + s + " comments");
            }
            Log.i("gg","post");
        }
    }

【问题讨论】:

    标签: android parse-platform android-asynctask


    【解决方案1】:

    doInBackground 方法中,您设置了一个侦听器,然后在不等待结果的情况下转到postExecute,因此您得到零。如果您正在使用的方法有任何同步版本,那么您应该使用它而不是 AsyncTask 内的回调版本,或者您可以忘记 AsyncTask 并从 UI 线程调用 findInBackground,如下所示:

     // here you set call back without waiting for result
    //so it could be used from UI thread instead of AsyncTask
    objectParseQuery.findInBackground(new FindCallback<ParseObject>() { 
            @Override
            public void done(List<ParseObject> objects, ParseException e) {
                if (e == null) {
                    Log.i("gg", "inside");
                    commentsView.setText("View all " + objects.size() + " comments");// set text here
                }
            }
    });
    

    【讨论】:

    • @Bahman 感谢您尝试回答。但是事情是执行后甚至在后台执行之前就已经执行了。我希望你能理解我。请查看我发布的日志。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    • 2020-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    • 2012-03-30
    相关资源
    最近更新 更多