ListView不在App Widget中显示数据

时间:2014-04-02 17:05:34

标签: android android-listview android-widget appwidgetprovider android-remoteview

我正在尝试为我的应用程序实现一个简单的收集小部件,我试图在小部件中显示ListView,但ListView在加载时停留...并且永远不会完成加载,我在数据库中有数据,列表视图仍然坚持使用它。

这是我的AppWidgetProvider类中的onUpdate方法:

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    int length = appWidgetIds.length;
    for (int x = 0; x < length; x++){
        RemoteViews remote = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        Intent intent = new Intent(context,RViewsService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[x]);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        remote.setRemoteAdapter(R.id.list,intent);
        remote.setEmptyView(R.id.list,R.id.empty);
        remote.setTextViewText(R.id.title,Utilities.setDayOfWeek());
        ////////////////////////////////////////////////////////////////////////////////////////////////////////
        Intent AddmateriaIntent = new Intent(context,AddMateria.class);
        PendingIntent pendingItentForButtonAddMateria = PendingIntent.getActivity(context,0,AddmateriaIntent,0);
        remote.setOnClickPendingIntent(R.id.add,pendingItentForButtonAddMateria);
        remote.setOnClickPendingIntent(R.id.empty, pendingItentForButtonAddMateria);
        appWidgetManager.updateAppWidget(appWidgetIds[x],remote);
    }
   super.onUpdate(context,appWidgetManager,appWidgetIds);
}

我可以检查并且方法onUpdate完成其工作而没有错误。 这里是RemoteViewService和RemoteViewsFactory,他们也没有在logCat上显示错误。

公共类RViewsService扩展了RemoteViewsService {

@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
    return new RViewFactory(getApplicationContext(),intent);
}

class RViewFactory implements RemoteViewsFactory {

    private DBAdapter adapter;
    private ArrayList<Materia> materias;
    private Context context;

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

    @Override
    public void onCreate() {
        adapter = new DBAdapter(context).openReadableDatabase();
        materias = new ArrayList<Materia>();
        Cursor cursor = adapter.query(Utilities.setDayAndTable(), new String[]{DBAdapter.ROW_SUBJECT,DBAdapter.ROW_COLOR_NAME,DBAdapter.ROW_COLOR_HEX}, null, null, null, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                while (!cursor.isAfterLast()) {
                    Materia materia = new Materia();
                    materia.setSubject(cursor.getString(cursor.getColumnIndex(DBAdapter.ROW_SUBJECT)));
                    materia.setColor(new ColorObject(cursor.getString(cursor.getColumnIndex(DBAdapter.ROW_COLOR_NAME)),
                            (cursor.getInt(cursor.getColumnIndex(DBAdapter.ROW_COLOR_HEX)))));
                    materias.add(materia);
                    Log.d("log", "Data: " + materia.getSubject());
                    cursor.moveToNext();
                }
            }
            cursor.close();
        }
    }

    @Override
    public void onDataSetChanged() {

    }

    @Override
    public void onDestroy() {
        materias = null;
        adapter.close();
    }

    @Override
    public int getCount() {
        return materias.size();
    }

    @Override
    public RemoteViews getViewAt(int i) {
        RemoteViews remote = new RemoteViews(context.getPackageName(),android.R.layout.simple_list_item_1);
        remote.setTextViewText(android.R.id.text1,materias.get(i).getSubject());
        remote.setTextColor(android.R.id.text1,materias.get(i).getColor().getColor());
        return remote;
    }

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

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

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

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

}

在LogCat中我无法在加载Widget时发现错误或警告,它只显示何时正在读取光标(我已经从LogCat中删除了文本,因为它与GC有关)。

logcat的

05-02 12:54:30.796  21885-21885/schooltimetable.app D/cannon﹕ Data: Math
05-02 12:54:30.806  21885-21885/schooltimetable.app D/cannon﹕ Data: Theory

我确定Cursor返回de数据,因为我已经在Logcat中记录它并且它如上所示。

这是声明AppWidgetProvider和RemoteViewService的 Manifest 代码段:

    <receiver android:name="schooltimetable.app.appWidgetProvider" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/cannon_widget" />
    </receiver>
    <service android:name=".RViewsService" android:permission="android.permission.BIND_REMOTEVIEWS"/>

我在 Nexus 7(2012):Android 4.4.2

上测试我的应用

感谢。

2 个答案:

答案 0 :(得分:7)

getViewTypeCount的返回次数更改为1。

getViewTypeCount是工厂将返回的视图类型数。

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

答案 1 :(得分:2)

我遇到了同样的问题,因为我将自定义视图放在列表行中,如果有,请尝试删除它们。