-1
我有一个包含一个按钮和一个包含一些数据的列表的UI。当用户在列表加载之前点击按钮时,它会给出一个NPE。我添加了一个这样的空检查来防止崩溃。如何让我的用户界面加载速度更快,性能更好?
//here cardData is the List which needs load.
if (cardData != null) {
for (CardDisplayData display : cardData) {
// do something
}
}
我在的onCreate
new FetchData().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR).get();
末这样做我删除空校验和更换执行者。现在,列表数据在打开UI之前加载,并且不再有崩溃。但是这会降低性能,因为用户界面先加载然后实现按钮侦听器。
我的异步任务,
private class FetchData extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
displayData(); // here is the data that needs to be displayed
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
try {
Collections.sort(cardData);
} catch (IllegalStateException ise) {
LOG.warn("Illegal state exception, ignoring it {}", ise);
}
PrintStockAdapter adapter = new PrintStockAdapter(getApplicationContext(), cardData);
details.setAdapter(adapter); //details is a listView
adapter.notifyDataSetChanged();
}
}
邮政问题 –
您的活动代码可能你请在这里发表您完整的活动代码? –
@KatieKat检查我的答案 –