我正在尝试与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);
也许它有点脏,但它确实起作用了。如果您认为有更好的解决方案,请告诉我们。)
答案 0 :(得分:0)
这必须是因为在oncreate中,volley加载数据并花费一些时间来加载数据......直到那时所有的updateUI功能都在列表中没有数据的情况下执行..在你拉动刷新之前,数据已被加载截击请求,它显示在列表中。