Android显示在从后端获取值之前进行渲染
我对安卓有点陌生,正试图用parse作为后端开发自己的应用程序。在这里,我试图从parse中获取一篇文章的评论数。 当我使用asynctask进行回调时,UI中的数据甚至在数据返回之前就被呈现出来,显示我的所有注释为零,即使它们不是。当我调试时,我得到了正确的大小,但它甚至在此之前就已经显示出来了。 当我尝试使用下面代码中所示的日志进行调试时,我就是这样得到调试的Android显示在从后端获取值之前进行渲染,android,parse-platform,android-asynctask,Android,Parse Platform,Android Asynctask,我对安卓有点陌生,正试图用parse作为后端开发自己的应用程序。在这里,我试图从parse中获取一篇文章的评论数。 当我使用asynctask进行回调时,UI中的数据甚至在数据返回之前就被呈现出来,显示我的所有注释为零,即使它们不是。当我调试时,我得到了正确的大小,但它甚至在此之前就已经显示出来了。 当我尝试使用下面代码中所示的日志进行调试时,我就是这样得到调试的 2020-04-21 13:53:58.984 22308-22589/com.project I/gg: outside 2020
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");
}
}
在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
}
}
});
@谢谢你的回答。但问题是,即使在后台执行之前,后期执行也会被执行。我希望你能理解我。请看我贴的日志。