Listview 未在 onCreate 中填充来自 Volley 的数据

Listview not filled with data from Volley in onCreate

我正在尝试使用 Volley 和 ORMLite 从数据中生成列表视图。当我执行下拉刷新或重新加载页面时,会显示数据。当我重新加载页面时,我使用的是 updateUI() 函数:

 private void updateUI() throws SQLException {
    mAdministrationList.clear();
    mAdminstrationAdapter = new AdministrationAdapter(this, getLayoutInflater(), mAdministrationList);
    mVolleyHandler.GETAdministrations();
    mAdministrationList = mAdministrationDao.queryForAll();


    mAdminstrationListView.setAdapter(mAdminstrationAdapter);
    mAdminstrationListView.setOnItemClickListener(this);
    mAdminstrationAdapter.notifyDataSetChanged();
}

此功能有效,但如上所述,列表仅在下拉刷新时加载。

我的 onCreate 看起来像这样:

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

    mAdminstrationListView = (ListView) findViewById(R.id.administrationListView);

    mProgressSpinner = (ProgressBar) findViewById(R.id.administration_spinner);

    try {
        mAdministrationDao = getHelper().getAdministrationDao();
        mGlobalsDao = getHelper().getGlobalsDao();
        mGlobals = mGlobalsDao.queryForAll().get(0);

        mCurrentAdministration = mGlobals.getAdministration();
        mToken = mGlobals.getToken();
        mUser = mGlobals.getUser();

        mVolleyHandler = VolleyHandler.getInstance(this, mGlobals.getIpAddress(), mCurrentAdministration, mUser, mToken);

        updateUI();
        mAdminstrationAdapter.notifyDataSetChanged();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.administrationSwipe);
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            try {
                updateUI();

                mAdminstrationAdapter.notifyDataSetChanged();
            } catch (SQLException e) {
                e.printStackTrace();
            }

            mSwipeRefreshLayout.setRefreshing(false);
        }
    });
}

我正在制作的 VolleyCall 是从 API 获取数据并将其解析为管理对象,该管理对象使用 ORMLite 保存到数据库中。

  public void ParseAdministration(String response) throws JSONException, SQLException {
    JSONObject jsonObject = new JSONObject(response);
    JSONArray results = jsonObject.getJSONArray("results");

    for (int i = 0; i < results.length(); i++) {

        JSONObject object = results.getJSONObject(i);

        Administration administration = new Administration();
        administration.setId(Long.valueOf(object.getString("id")));
        administration.setName(object.getString("name"));
        administration.setAddress1(object.getString("address1"));
        administration.setAddress2(object.getString("address2"));
        administration.setPostalCode(object.getString("postalCode"));
        administration.setCity(object.getString("city"));
        administration.setCountry(object.getString("country"));

        mDatabaseHelper.saveAdministration(administration);

    }
}

我找不到问题出在哪里,所以也许你们中的一些人知道我的问题是什么。

提前致谢

已编辑:

感谢 Shubham Goel,我找到了解决方案:

 new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    mProgressSpinner = (ProgressBar) findViewById(R.id.administration_spinner);
                    updateUI();
                } catch (SQLException e) {
                    e.printStackTrace();
                }

                mProgressSpinner.setVisibility(View.GONE);
            }
        }, 3000);

也许它有点脏,但它完成了工作。如果您认为有更好的解决方案,请告诉我:)

这一定是因为在 oncreate 中 volley 加载数据并需要一些时间来加载数据...直到那时所有 updateUI 功能都在列表中没有数据的情况下执行..在您拉动刷新之前数据已经加载通过截击请求,它显示在列表中。