正确实施与列表视图主屏幕widget的

问题描述 投票:0回答:1

我试图创建一个从SQLite数据库加载信息的控件。我的问题是,当我运行的代码。所有编译罚款,但小部件总是空空的。 (SQLite数据库具有信息)

widget_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ListView
    android:id="@+id/widget_listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary" />

widget_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="72dp"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    >
    <TextView
        android:id="@+id/stock_symbol"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="start|center_vertical"
        tools:text="List Item"
        />
</LinearLayout>

控件提供者

public class ToDoTickWidget extends AppWidgetProvider {

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                            int appWidgetId) {
    // Construct the RemoteViews object
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_main);

    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget(appWidgetId, views);
}     

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // There may be multiple widgets active, so update all of them
    for (int appWidgetId : appWidgetIds) {
        updateAppWidget(context, appWidgetManager, appWidgetId);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

@Override
public void onEnabled(Context context) {
    // Enter relevant functionality for when the first widget is created
}

@Override
public void onDisabled(Context context) {
    // Enter relevant functionality for when the last widget is disabled
}
}   

最后,工厂

public class ListViewRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {

    private Context context;
    private Cursor cursor;
    private Intent intent;

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

    private void  initCursor(){
        if (cursor!=null){
            cursor.close();
        }
        final long identityToken = Binder.clearCallingIdentity();
        cursor = new ListFacade(context).RowsCursor();
        Binder.restoreCallingIdentity(identityToken);    }

    @Override
    public void onCreate() {
        initCursor();
        if (cursor!=null){
            cursor.moveToFirst();

        }
    }


    @Override
    public void onDataSetChanged() {
        initCursor();
    }

    @Override
    public void onDestroy() {
        cursor.close();
    }

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

    @Override
    public RemoteViews getViewAt(int position) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_item);

        cursor.moveToPosition(position);
        System.out.println("--->"+cursor.getCount()+"<---");
        remoteViews.setTextViewText(R.id.stock_symbol, cursor.getString(1));

        return remoteViews;
    }

    @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 true;
    }
}

如何到光标在一个SQLite外观类。

public Cursor RowsCursor(){
        SQLiteDatabase conn = new SqliteHandler(context).getReadableDatabase();
        Cursor cursor =  conn.rawQuery("SELECT ROWID,* from TODO_LIST", null);
        return cursor;
    }

当我运行这段代码,我得到一个空的小部件。

enter image description here

android android-listview android-sqlite android-widget
1个回答
0
投票

我找到了解决办法。只有我需要调用的onUpdate()函数的服务。

这条路:

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        for (int appWidgetId : appWidgetIds) {
            RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.widget_main);
            Intent intent = new Intent(context, ListViewWidgetService.class);
            views.setRemoteAdapter(R.id.widget_listView,intent);
            appWidgetManager.updateAppWidget(appWidgetId,views);
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.