简体   繁体   中英

Error in notifyDataSetChanged ListView Adapter File

I am getting the following error in hundreds of my users. I used it in my code because it asked me to call notifyDataSetChanged() but it didn't work. I have shared my adapter file below. I've done a lot of research for this but can't pinpoint the problem. I need your help.

Regards.

Fatal Exception: java.lang.IllegalStateException
The content of the adapter has changed but ListView did not receive a notification. 

Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131296786, class android.widget.ListView)

My Adapter Code:

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) {
        }
    }
}

Here is what I think the problem is, let us first see your code. This portion right here

public TracksAdapter(Context context, List<Track> rowItems) {
        notifyDataSetChanged();
        this.context = context;
        this.trackItems = rowItems;
    }

As you said you are calling notifyDataSetChanged() , but the thing is where is it being called.

You are calling it right now before any changes to data set has been made, you should call it after you have assigned the rowItems . You call it before and at that point nothing has changed.

So the correct code, will be as follows:

public TracksAdapter(Context context, List<Track> rowItems) {
        this.context = context;
        this.trackItems = rowItems;
        notifyDataSetChanged();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM