![](/img/trans.png)
[英]Android ListView Adapter error calling notifyDataSetChanged, Android bug?
[英]Error in notifyDataSetChanged ListView Adapter File
我在數百名用戶中收到以下錯誤。 我在我的代碼中使用了它,因為它要求我調用 notifyDataSetChanged() 但它不起作用。 我在下面分享了我的適配器文件。 我為此做了很多研究,但無法確定問題所在。 我需要你的幫助。
問候。
Fatal Exception: java.lang.IllegalStateException
The content of the adapter has changed but ListView did not receive a notification.
確保適配器的內容不是從后台線程修改的,而只是從 UI 線程修改的。 確保您的適配器在其內容更改時調用 notifyDataSetChanged()。 [在列表視圖中(2131296786,class android.widget.ListView)
我的適配器代碼:
public class TracksAdapter extends BaseAdapter {
private DatabaseReference mDatabaseReference;
Context context;
private static TracksAdapter mInstance = null;
private Activity activity;
private InterstitialAd interstitialAds;
private int pos = -1;
private LoadingDialog progress = null;
List<Track> trackItems;
public void showProgressDialog() {
progress = new LoadingDialog(context);
progress.setCircleColors(context.getResources().getColor(R.color.turu), context.getResources().getColor(R.color.turu), context.getResources().getColor(R.color.turu));
progress.show();
}
public void hideProgressDialog() {
if (progress != null) {
progress.dismiss();
progress = null;
}
}
protected void TracksAdapter(Activity activity) {
this.activity = activity;
}
public TracksAdapter(Context context, List<Track> rowItems) {
notifyDataSetChanged();
this.context = context;
this.trackItems = rowItems;
}
@Override
public int getCount() {
return trackItems.size();
}
@Override
public Object getItem(int position) {
pos = position;
if (trackItems != null && trackItems.size() > position) {
return trackItems.get(position); // Line 54.
}
return null;
}
@Override
public long getItemId(int position) {
return trackItems.indexOf(getItem(position));
}
/* private view holder class */
private class ViewHolder {
String id, names, phones, lastseens, dates;
TextView name;
TextView phone;
TextView lastseen;
Integer membership;
ImageView delete, file;
}
public static TracksAdapter getInstance() {
return mInstance;
}
@RequiresApi(api = Build.VERSION_CODES.O)
@SuppressLint("WrongViewCast")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
}
}
}
這就是我認為的問題所在,讓我們先看看你的代碼。 這部分就在這里
public TracksAdapter(Context context, List<Track> rowItems) {
notifyDataSetChanged();
this.context = context;
this.trackItems = rowItems;
}
正如您所說,您正在調用notifyDataSetChanged()
,但問題是它在哪里被調用。
您現在在對數據集進行任何更改之前調用它,您應該在分配rowItems
之后調用它。 您之前調用它,那時沒有任何改變。
所以正確的代碼如下:
public TracksAdapter(Context context, List<Track> rowItems) {
this.context = context;
this.trackItems = rowItems;
notifyDataSetChanged();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.