Homescreen widget, listView shows “Loading”

让人想犯罪 __ 提交于 2020-01-02 00:54:34

问题


To start off, an image says thousand words:

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


回答1:


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




回答2:


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.




回答3:


I had to remove

android:minHeight="?attr/dropdownListPreferredItemHeight"

from the layout of the list item to make this work




回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/21891010/homescreen-widget-listview-shows-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!