Question

To start off, an image says thousand words:

enter image description here

This takes place although getViewAt is called 4 items as my cursor size is.

Here's the code:

public class WidgetService extends RemoteViewsService {
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        System.out.println("Factory");
        return(new WidgetViewsFactory(this.getApplicationContext(), intent));
    }
}

The widget provider:

public class WidgetViewsFactory implements RemoteViewsService.RemoteViewsFactory, LNTConstants
{   
    private Context context = null;

    ArrayList<DBItemModel> items;

    public WidgetViewsFactory(Context context, Intent intent) {
        this.context = context;

        Cursor c = Items.get(context, where);
        items = Items.getFromCursor(c);
    }

    @Override
    public void onCreate() {
        /*

        */
    }

    @Override
    public void onDestroy() {

    }

    @Override
    public int getCount() {
        if(items != null) {
            System.out.println("Count: " + items.size());
            return items.size();
        }
        return 0;
    }

    @Override
    public RemoteViews getViewAt(int position) {
        System.out.println("getViewAt: " + position);
        ItemBean item = items.get(position);

        RemoteViews row = new RemoteViews(context.getPackageName(), R.layout.item_list);
        row.setTextViewText(R.id.tvListItem, item.summary);

        System.out.println("Widget item title: " + item.summary);

        return row;
    }

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

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

    @Override
    public long getItemId(int position) {
        return position;
    }

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

    @Override
    public void onDataSetChanged() {
        // no-op
    }
}

Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:descendantFocusability="blocksDescendants">

    <TextView
        android:id="@+id/tvListItem"
        style="@style/text_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:minHeight="?attr/dropdownListPreferredItemHeight" />

</RelativeLayout>

My 4 items definitely don't have the text loading and i don't know where that is coming from.

Could someone point out what am doing wrong?

Thanks

Was it helpful?

Solution 3

I had to remove

android:minHeight="?attr/dropdownListPreferredItemHeight"

from the layout of the list item to make this work

OTHER TIPS

Check if you are using a non-supported view.

In my case, I was using View in my layout as a divider. and didn't realize it's not one of the supported views.

I just removed it and the widget behaved as expected.

This answer helped me https://stackoverflow.com/a/23122006/2051381

I had to return 1 instead of 0 from the method getViewTypeCount() of my remoteViewsFactory class

I also came here to say this: I set the background of the list item to
?attr/selectableitembackground

If someone having the same issue like this one and if you're using a custom launcher try to use the default launcher instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top