在窗口小部件中使用ListView会导致“问题加载窗口小部件”

时间:2019-05-19 10:29:35

标签: android listview widget android-linearlayout remoteview

在我的窗口小部件中,我可以将带有“ LinearLayout”的适配器添加为窗口小部件xml文件的“ LinearLayout”中的行,但是当将其添加到“ ListView”时,无法加载该窗口小部件。

如果我注释掉

remoteViews.addView(R.id.widget_LinearLayout_viewContainer, remoteViewsAdapter);

没有内容(当然),但是显示正确。

我认为,“ LinearLayout”应该可添加到“ ListView”(或者我错了吗?)。最糟糕的是,logcat什么也没说。

Widget.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget_LinearLayout_background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBlack"
    android:orientation="vertical">

    <!-- using ListView here causes "Problem loading widget" -->
    <LinearLayout
        android:id="@+id/widget_LinearLayout_viewContainer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />

    <TextView
        android:id="@+id/widget_TextView_emptyList"
        style="@android:style/TextAppearance.Small"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="2dip"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/widget_TextView_NoData"
        android:textColor="@color/colorGrayLight" />

</LinearLayout>

Widget_Adapter.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_marginLeft="2sp"
    android:layout_marginRight="2sp"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

        <TextView
            android:id="@+id/widget_adapter_TextView_headline"
            android:textStyle="bold"
            android:maxLines="1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />

        <TextView
            android:id="@+id/widget_adapter_TextView_info"
            android:maxLines="1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />

</LinearLayout>

Widget.java

public class Widget extends AppWidgetProvider {

    @Override
    public void onReceive(Context context, Intent intent) {

        super.onReceive(context, intent);

        ... // fetching data to cursor here (no errors)

        // create view
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);

        if (cursor.getCount() == 0) {

            // no data, so hide ListView in XML
            remoteViews.setViewVisibility(R.id.widget_LinearLayout_viewContainer, View.GONE);

        } else {

            // there are entries, so hide TextView "currently no data" in XML
            remoteViews.setViewVisibility(R.id.widget_TextView_emptyList, View.GONE);

            cursor.moveToFirst();

            do {

                // create new view for date-item from widget_list_item.xml
                RemoteViews remoteViewsAdapter = new RemoteViews(context.getPackageName(), R.layout.widget_adapter);

                // get current dataset
                MainActivityAdapter mainActivityAdapter = new MainActivityAdapter(context, cursor, sqLiteDatabase);
                String itemInfo = mainActivityAdapter.getItemInfo(cursor.getPosition());

                //set text
                remoteViewsAdapter.setTextViewText(R.id.widget_adapter_TextView_headline, cursor.getString(cursor.getColumnIndex(Database.COL_NAME)));
                remoteViewsAdapter.setTextViewText(R.id.widget_adapter_TextView_info, itemInfo);

                // ERROR? uncomment addView displays correct, but empty (of course)
                // Works with LinearLayout, but not with ListView
                remoteViews.addView(R.id.widget_LinearLayout_viewContainer, remoteViewsAdapter);

            } while (cursor.moveToNext());
        }

        cursor.close();
        database.close();

        // Trigger widget update
        AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(context, Widget.class), remoteViews);
    }
}

0 个答案:

没有答案