Search code examples
androidlistviewandroid-widgetremoteview

Android Widget Collection: error in the order of the Listview (RemoteViews)


I'm working on a Widget to show a list of Events with dates. The problem occurs when the list is filled up with getViewAt, wicht is call in different order and also a few times.

In the list lEvents, the order is correct.

This is my WidgetRemoteViewsFactory:

public class WidgetRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {

private Context mContext;
private List<EventItem> lEvents;

private DBHandler db;

private String sCurrentYear = null;

public WidgetRemoteViewsFactory(Context applicationContext, Intent intent) {
    mContext = applicationContext;
}

@Override
public void onCreate() {

    // Recuperar la Base de Datos
    if (db == null) { db = new DBHandler(mContext); }
    lEvents = db.getAllEventsToday();
}

@Override
public void onDataSetChanged() {
    // No se que coño hace
    final long identityToken = Binder.clearCallingIdentity();
    Binder.restoreCallingIdentity(identityToken);
}

@Override
public void onDestroy() {
}

@Override
public int getCount() {
    return lEvents == null ? 0 : lEvents.size();
}

@Override
public RemoteViews getViewAt(int position) {
    if (position == AdapterView.INVALID_POSITION ||
            lEvents == null) {
        return null;
    }

    EventItem oEvent = lEvents.get(position);
    RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_list_item);
    if(sCurrentYear == null || !sCurrentYear.equals(String.valueOf(oEvent.getDateEventYear()))) {
        sCurrentYear = String.valueOf(oEvent.getDateEventYear());
        rv.setTextViewText(R.id.widgetEventYear, oEvent.getDateEventWithFormat(mContext.getString(R.string.widget_date_list_format)));
        rv.setViewVisibility(R.id.widgetEventYear, View.VISIBLE);
    }
    rv.setTextViewText(R.id.widgetEventContent, oEvent.getContent());

    return rv;
}

@Override
public RemoteViews getLoadingView() {
    return null;
}

@Override
public int getViewTypeCount() {
    return 1;
}

@Override
public long getItemId(int position) {
    return position;
    //EventItem oEvent = lEvents.get(position);
    //return (oEvent != null) ? oEvent.getId() : position;
}

@Override
public boolean hasStableIds() {
    return true;
}
}

Thank you so much!!


Solution

  • Finally I found the answer: YES, it is call in diferent order (and sometimes, to mesure the height, few times the same position), but android left them in the right order.

    So, my error was to use a class variable (sCurrentYear) to know if we are in a new year:

    sCurrentYear == null || !sCurrentYear.equals(String.valueOf(oEvent.getDateEventYear()))
    

    To fix the problem, and group every event with his year, you have to get the before object in the list and compare with the current one:

    position <= 0 || oEvent.getDateEventYear() != lEvents.get(position-1).getDateEventYear()
    

    And thats all.

    P.S.: Also, you can check this google video to learn more about the ListView. https://www.youtube.com/watch?v=wDBM6wVEO70&t=40m45s