Homescreen widget, listView shows "Loading"
Asked Answered
J

6

16

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

Jeffryjeffy answered 19/2, 2014 at 19:56 Comment(0)
J
9

I had to remove

android:minHeight="?attr/dropdownListPreferredItemHeight"

from the layout of the list item to make this work

Jeffryjeffy answered 19/2, 2014 at 23:5 Comment(1)
Man, you are like God to me in this momentPartly
W
20

This answer helped me https://mcmap.net/q/747549/-android-widget-with-listview-doesn-39-t-load-items-correclty

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

Waxwing answered 23/6, 2015 at 19:37 Comment(0)
M
20

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.

Mercantile answered 9/8, 2017 at 1:44 Comment(2)
If you're using a ListView, you can use its divider attribute to achieve the same.Balthasar
I know that, but I copied the layout from recycled view and didn't consider that View won't be supported.Mercantile
J
9

I had to remove

android:minHeight="?attr/dropdownListPreferredItemHeight"

from the layout of the list item to make this work

Jeffryjeffy answered 19/2, 2014 at 23:5 Comment(1)
Man, you are like God to me in this momentPartly
F
1

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

Frye answered 11/4, 2018 at 20:56 Comment(0)
L
0

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

Lema answered 3/10, 2019 at 6:58 Comment(0)
M
0

Another idea, worked in my case. Make sure that XML of the item element is valid. I missed adding required android:layout_width and android:layout_height attributes to one of elements of the list item view and achieved the exact effect of getting the indefinite "Loading..." screen.

Mcmillin answered 8/5 at 7:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.