使用listview正确实现主屏幕小部件

时间:2019-02-07 00:34:41

标签: android android-listview android-sqlite android-widget

我正在尝试创建一个包含从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

1 个答案:

答案 0 :(得分: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);
        }
    }