作者:炙天痕_953 | 来源:互联网 | 2023-09-24 12:15
我有一个用json填充并使用自定义适配器的ListView。
这是我在Zoznamactivity中缩短的代码:
public class Zoznam extends AppCompatactivity{
private ArrayList actorsList;
private actorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_filter);
final ListView lv = findViewById(R.id.listView1);
actorsList = new ArrayList<>();
adapter = new actorAdapter(this,"Zoznam",actorsList);
lv.setadapter(adapter);adapter.notifyDataSetChanged();
new getcontacts(Zoznam.this).execute("all","all");
private static class getcontacts extends AsyncTask {
ProgressDialog dialog;
private final WeakReference activityReference;
getcontacts(Zoznam context) {
activityReference = new WeakReference<>(context);
}
@Override
protected void onPreExecute() {
Zoznam activity = activityReference.get();
if (activity == null || activity.isFinishing()) return;
super.onPreExecute();
dialog = new ProgressDialog(activity);
dialog.setMessage(activity.getResources().getString(R.string.Loading));
dialog.setTitle(activity.getResources().getString(R.string.connecting));
dialog.show();
dialog.setCancelable(false);
}
@Override
protected String doInBackground(String... sText1) {final Zoznam activity = activityReference.get();
HttpHandler sh = new HttpHandler();
String url = "URL";
String jsOnStr= sh.makeServiceCall(url);
if (jsonStr != null) {
try {JSONObject jsOnObj= new JSONObject(jsonStr);
JSONArray actors = jsonObj.getJSONArray("result");
for (int i = 0; i JSONObject c = actors.getJSONObject(i);
actors actor = new actors();
actor.setLetter(c.getString("letter"));
actor.setNazov(c.getString("nazov"));
actor.setThumb(c.getString("thumb"));
activityReference.get().actorsList.add(actor);
}
} catch (final JSONException e) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity,R.string.Nodata,Toast.LENGTH_LONG).show();
}
}); }
return jsonStr;
} else {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity,R.string.Network,Toast.LENGTH_LONG).show();
}
});
return null;
}
}
protected void onPostExecute(String result) {
Zoznam activity = activityReference.get();
if (activity == null || activity.isFinishing()) return;
dialog.dismiss();
activity.adapter.notifyDataSetChanged();
super.onPostExecute(result);
}
}
protected void onResume() {
super.onResume();
adapter.notifyDataSetChanged();
}
问题是,在崩溃报告中我发现了此错误:
适配器的内容已更改,但ListView没有收到通知。确保不从后台线程修改适配器的内容,而仅从UI线程修改。确保您的适配器在其内容更改时调用notifyDataSetChanged()。
但是,仅在一台设备上发生了一次,但是我想解决此问题。
我已经阅读了更多关于此的主题,但是我仍然没有解决方案。正如您在我的代码中看到的那样,我也在onPostExecute上调用notifyDataSetChanged(),所以不确定问题出在哪里。
您可能得到了例外,因为您在后台线程上更新了适配器列表。
activityReference.get().actorsList.add(actor);
doInBackground
方法中的这一行正在更新您传递给适配器的实际列表。 doInBackground
中的所有这些计算必须非常快地进行,并且来自notifyDataSetChanged
的{{1}}被调用。我不确定如何,但是这一次没有及时调用您的onPostExecute
,因此您的列表已更新,但是您的onPostExecute
没有收到通知。
您可以在ListView
中创建本地列表,并在AsyncTask
中更新活动的列表。示例代码-
onPostExecute
这应该可以,但是您可以通过使用回调进一步改进代码。这样,您将不必在asyncTask内保留活动的引用,并且可以将所有逻辑移出asyncTask。 https://stackoverflow.com/a/15693380/6168272这个答案应该很有帮助。
,
更改AsyncTask
,以便计算doInBackground()
中的列表,然后将其返回到onPostExecute()
,然后在那里进行所有更新:
// change the type parameters to return a List
private static class GetContacts extends AsyncTask> {
@Override
protected List doInBackground(Void... ignored) {
// add this at the top
List actors = new ArrayList<>();
// inside your loop,replace this line:
// activityReference.get().actorsList.add(actor);
// with this instead
actors.add(actor);
// and at the end,return the list
return actors;
}
// the list you returned from doInBackground() is passed here
protected void onPostExecute(List result) {
// update your activity all at once in this method
activity.actorsList.clear();
activity.actorsList.addAll(result);
activity.adapter.notifyDataSetChanged();
}
}
我整理了一些代码以使内容更易于阅读,但总的思路就在那里。