Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView adapter data change without ListView being notified

I've written a ListActivity that has a custom list adapter. The list is being updated from a ContentProvider when onCreate is run. I also have a service that gets started when I run the app and it first updates the ContentProvider, then sends a Broadcast that the content has been updated.
My ListActivity receives the broadcast and tries to update my ListView. My problem is, I'm getting intermittent errors about the ListView adapter data changing without the ListView being notified. I call the notifyDataSetChanged() method on my list adapter right after I update it. What it seems like is happening is the list is still in the process of being updated after first call in onCreate when it receives the broadcast from the service to update, so it tries to update my ListView before it's finished updating from it's first run. Does this make sense? Here is some of my code.

NOTE: The service is working properly, it gets new data and updates my ContentProvider, and I do get the broadcast in my activity when it is updated.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ctx = this;
    getPrefs();
    setContentView(R.layout.main);

    // Setup preference listener
    preferences = PreferenceManager.getDefaultSharedPreferences(this);
    preferences.registerOnSharedPreferenceChangeListener(listener);


    // Setup report list adapter
    ListView nzbLv = (ListView) findViewById(R.id.report_list);
    nzbla = new NZBReportListAdaptor(ctx);
    getReports();
    nzbla.setListItems(report_list);            
    nzbLv.setAdapter(nzbla);        
    // Broadcast receiver to get notification from NZBService to update ReportList
    registerReceiver(receiver,
            new IntentFilter(NZBService.BROADCAST_ACTION));

    startService(new Intent(ctx, NZBService.class));
}

@Override
public void onResume() {
    super.onResume();
    timerHandler.resume();      
new updateSabQueue().execute();
    //updateList();
}

@Override
public void onPause() {
    super.onPause();
    timerHandler.pause();
    unregisterReceiver(receiver);
}


private BroadcastReceiver receiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(ctx, "NZBService broadcast recieved", Toast.LENGTH_SHORT).show();
        updateReportList();
    }
};


private void updateReportList() {
    new updateReportList().execute();
}



private class updateReportList extends AsyncTask<Void, Void, Boolean> {

    /* (non-Javadoc)
     * @see android.os.AsyncTask#onPreExecute()
     * Show progress dialog
     */
    protected void onPreExecute() {
    }

    /* (non-Javadoc)
     * @see android.os.AsyncTask#doInBackground(Params[])
     * Get new articles from the internet
     */
    protected Boolean doInBackground(Void...unused) {
        getReports();
        return true;
    }

    /**
     * On post execute.
     * Close the progress dialog
     */
    @Override
    protected void onPostExecute(Boolean updated) {
        if (updated) {
            Log.d(TAG, "NZB report list adapter updated");
            synchronized(this) {
                nzbla.setListItems(report_list);            
            }
            Log.d(TAG, "NZB report list notified of change");
            nzbla.notifyDataSetChanged();                           
        }
    }
}

Now that this question is answered, I will post my updated code in an effort to help others who might come across it.

@Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  ctx = this;
    getPrefs();
setContentView(R.layout.main);

    // Setup preference listener
    preferences = PreferenceManager.getDefaultSharedPreferences(this);
    preferences.registerOnSharedPreferenceChangeListener(listener);

    // Setup report list adapter
    ListView nzbLv = (ListView) findViewById(R.id.report_list);
    nzbla = new NZBReportListAdaptor(ctx);
    report_list.addAll(getReports());
    nzbla.setListItems(report_list);            
    nzbLv.setAdapter(nzbla);        
    // Broadcast receiver to get notification from NZBService to update ReportList
    registerReceiver(receiver,
            new IntentFilter(NZBService.BROADCAST_ACTION));

    startService(new Intent(ctx, NZBService.class));
}


private class updateReportList extends AsyncTask<Void, Void, ArrayList<Report>> {

    /* (non-Javadoc)
     * @see android.os.AsyncTask#onPreExecute()
     * Show progress dialog
     */
    protected void onPreExecute() {
    }

    /* (non-Javadoc)
     * @see android.os.AsyncTask#doInBackground(Params[])
     * Get new articles from the internet
     */
    protected ArrayList<Report> doInBackground(Void...unused) {
        return getReports();
    }

    /**
     * On post execute.
     * Close the progress dialog
     */
    @Override
    protected void onPostExecute(ArrayList<Report> updated) {
        nzbla.setListItems(updated);            
        nzbla.notifyDataSetChanged();                           
    }
}


private ArrayList<Report> getReports() {
    ArrayList<Report> reports = new ArrayList<Report>();
    ContentResolver r = getContentResolver();
    Cursor c = r.query(NZBReportProvider.CONTENT_URI, null, null, null, NZBReportProvider.ARTICLE_KEY_ROWID + " DESC");
    startManagingCursor(c);
    Log.d(TAG, "NZBReport cursor.getCount=" + c.getCount());
    int title = c.getColumnIndex(NZBReportProvider.ARTICLE_KEY_TITLE);
    int desc = c.getColumnIndex(NZBReportProvider.ARTICLE_KEY_DESCRIPTION);
    int cat = c.getColumnIndex(NZBReportProvider.ARTICLE_KEY_CAT);
    int size = c.getColumnIndex(NZBReportProvider.ARTICLE_KEY_SIZE);
    int link = c.getColumnIndex(NZBReportProvider.ARTICLE_KEY_LINK);
    int catid = c.getColumnIndex(NZBReportProvider.ARTICLE_KEY_CATID);
    int date = c.getColumnIndex(NZBReportProvider.ARTICLE_KEY_DATE_ADDED);
    int group = c.getColumnIndex(NZBReportProvider.ARTICLE_KEY_GROUP);

    if (c.getCount() > 0) {
        c.moveToFirst();
        do {
            URL url = null;
            try {
                url = new URL(c.getString(link));
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            reports.add(new Report(c.getString(title), url, c.getString(desc), c.getString(cat), c.getString(date), c.getString(size), c.getInt(catid), c.getString(group)));               
        } while (c.moveToNext());                   
    }
    return reports;
}
like image 214
brockoli Avatar asked Jan 08 '11 23:01

brockoli


1 Answers

You have to do all your updating of Adapter data on the UI thread so the synchronized block isn't necessary. It's also useless since your synchronizing on the AsyncTask which is created new each time it's executed.

Another problem is that you are calling notifyDataSetChanged external to the Adapter. You should call it at the end of your setListItems method. That shouldn't be causing errors though since it's being executed on the UI thread but it should not be called in that way.

You should make sure that your getReports method is not modifying the backing store of the Adapter in any way. Since it is running on a separate thread it cannot modify anything the Adapter has access too. Even if it's protected by locks. What you need to do is in your doInBackground method is generate the list of updates or a new list, etc., and pass that into onPostExecute which then commits the new data to the Adapter on the UI thread. So, if your getReports function is changing report_list and your Adapter has a reference to report_list you are doing it wrong. getReports must create a new report_list and then pass that back to your Adapter when it's finished creating it on the UI thread.

To reiterate, you can only modify data the Adapter and subsequently the ListView has access too on the UI thread. Using synchronization/locks does not change this requirement.

like image 103
Rich Schuler Avatar answered Oct 04 '22 13:10

Rich Schuler