【问题标题】:In onPostExecute() Get Arraylist size 0 where i Get actual size in doInbackground() method在 onPostExecute() Get Arraylist size 0 where i Get actual size in doInbackground() 方法
【发布时间】:2016-05-20 07:38:52
【问题描述】:

我知道这个问题问了很多次,但我找不到错误

我检查了所有问题,但我找不到我做错了什么。

我正在使用 back4App 作为后端。

我只是从中检索数据,只想显示在我的列表视图中。

我设置了日志来检查我正在检索的数据,因此它在 doInbackground 方法中显示完美的数组列表大小。但是当我将它返回到 postexecute 并且在那里我也检查它的大小时。但它在那里显示 0。 不知道自己做错了什么。

我的代码如下:

public class HomeActivity extends AppCompatActivity {
ProgressDialog mProgressDialog;
ArrayList<String> items;
ListView listView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);


    listView = (ListView) findViewById(R.id.listview);
    items=new ArrayList<>();
    new GetServices().execute();


}




private class GetServices extends AsyncTask<Void, Void, ArrayList<String>> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(HomeActivity.this);
        // Set progressdialog title
        mProgressDialog.setTitle("Parse.com Simple ListView Tutorial");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();

    }

    @Override
    protected ArrayList<String> doInBackground(Void... params) {
        // Locate the class table named "services" in Parse.com
        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("services");
        query.orderByAscending("updatedAt");


        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> objects, ParseException e) {
                if (e == null) {
                    items.clear();
                    Log.i("TAG", "datasize=>" + objects.size());
                    List<String> temp = new ArrayList<String>();

                    for (ParseObject services : objects) {
                        temp.clear();
                        items.add(services.getString("serviceTitle"));

                        temp = services.getList("serviceDetails");
                        items.add(Util.getHtmlText(temp));

                    }

                    Log.i("TAG", "itemsize=>" + items.size());

                } else {
                    e.printStackTrace();
                }
            }
        });



        return items;
    }

    @Override
    protected void onPostExecute(ArrayList<String> strings) {
        super.onPostExecute(strings);
        Log.i("TAG", "resultsize=>" + strings.size());

        for (int i = 0; i < strings.size(); i++) {
            Log.i("TAG", "==>" + strings.get(i));
        }
        // Pass the results into an ArrayAdapter
        CustomServiceListAdapter adapter = new CustomServiceListAdapter(HomeActivity.this, R.layout.custom_fragment_service_list_item, strings);


        // Binds the Adapter to the ListView
        listView.setAdapter(adapter);
        // Close the progressdialog
        mProgressDialog.dismiss();
        // Capture button clicks on ListView items
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // Send single item click data to SingleItemView Class
                Intent i = new Intent(HomeActivity.this,
                        DetailsActivity.class);
                // Pass data "name" followed by the position
                i.putExtra("data", items.get(position));
                // Open SingleItemView.java Activity
                startActivity(i);
            }
        });

    }
}}

我的 Logcat 显示如下:

05-20 13:10:28.661 19099-19099/com.example.apexweb I/TAG: resultsize=>0
05-20 13:10:29.409 19099-19099/com.example.apexweb I/TAG: datasize=>4
05-20 13:10:29.410 19099-19099/com.example.apexweb I/TAG: itemsize=>8

【问题讨论】:

  • items.clear() 在 doInBackground() 的 for 循环中?
  • 当你可以使用parse api在后台线程中查询事物时,为什么还需要asynctask?
  • 为什么在线程内调用findInBackground?你不能同步运行吗?

标签: android arraylist android-asynctask


【解决方案1】:

根据文档findInBackground 从后台线程中的源检索满足此查询的ParseObjects 列表,并在回调中获取结果。 doInBackground 不会等到带有结果的回调被调用。这就是为什么在 onPostExecute 中大小仍然为 0。由于 doInBackground 已经在不同的线程上运行,因此不需要第二级异步。您可以直接拨打find()。或者,您可以摆脱 AsyncTask 并使用来自 UI 线程的回调运行 findInBackground

【讨论】:

  • 非常感谢。
猜你喜欢
  • 2015-08-15
  • 2019-12-10
  • 1970-01-01
  • 2020-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多