窗口小部件listview在单击时打开活动

时间:2020-01-31 08:35:57

标签: android android-listview android-appwidget-list

我在该网站上进行了艰苦的搜索,以帮助我弄清楚这个问题,但是所有问题都没有帮助我。我使用了堆栈小部件示例来入门,但是单击以打开活动无法获取Listview项。当我单击小部件中的锻炼列表项时,我想将 workoutId 作为附加发送给 ViewWorkoutDetailActivity 。我为我所缺少的感到难过。 这是我的 WorkoutWidgetProvider

public class WorkoutWidgetProvider extends AppWidgetProvider {
private static SharedPreferences preferences;
private static SharedPreferences.Editor editor;
private static int id = 0;
public static final String EXTRA_ITEM = "com.udacity.stackwidget.EXTRA_ITEM";
public static final String OPEN_ACTIVITY_ACTION = "com.udacity.stackwidget.OPEN_ACTIVITY_ACTION";

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                            int appWidgetId) {

    id = appWidgetId;

    Intent intent = new Intent(context, WorkoutWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
    // Construct the RemoteViews object
    RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.workout_widget_provider);
    rv.setRemoteAdapter(appWidgetId, R.id.list_view, intent);
    rv.setEmptyView(R.id.list_view, R.id.empty_view);

    Intent activityIntent = new Intent(context, WorkoutWidgetProvider.class);
    activityIntent.setAction(WorkoutWidgetProvider.OPEN_ACTIVITY_ACTION);
    activityIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
    PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId,
            activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    rv.setPendingIntentTemplate(R.id.list_view, pendingIntent);
    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget(appWidgetId, rv);
}

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

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

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private static void setRemoteAdapter(Context context, @NonNull final RemoteViews views) {
    views.setRemoteAdapter(R.id.list_view,
            new Intent(context, WorkoutWidgetService.class));
}

public static void sendRefreshBroadcast(Context context) {
    Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    intent.setComponent(new ComponentName(context, WorkoutWidgetProvider.class));
    context.sendBroadcast(intent);
}

@Override
public void onReceive(final Context context, Intent intent) {
    final String action = intent.getAction();
    if(action.equals(OPEN_ACTIVITY_ACTION)){
        int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        int workoutId = intent.getIntExtra(EXTRA_ITEM, 0);
        Intent activityIntent = new Intent(context, ViewWorkoutDetailActivity.class);
        activityIntent.putExtra("workoutId", workoutId);
        activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(activityIntent);
    }
    super.onReceive(context, intent);
}

} 我的 WorkoutWidgetService

public class WorkoutWidgetService extends RemoteViewsService {
@Override
public RemoteViewsService.RemoteViewsFactory onGetViewFactory(Intent intent) {
    return new WorkoutRemoteViewsFactory(this.getApplicationContext(), intent);
}

}

WorkoutRemoteViewsFactory类实现RemoteViewsService.RemoteViewsFactory {

private Context context;
private Cursor cursor;
private Intent intent;
DbAdapter helper;
DbWorkout dbWorkout;
private int mAppWidgetId;

// For obtaining the activity's context and intent
public WorkoutRemoteViewsFactory(Context context, Intent intent) {
    this.context = context;
    this.intent = intent;
    helper = new DbAdapter(context);
    mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID);
}

private void initCursor(){
    if (cursor != null) {
        cursor.close();
    }
    final long identityToken = Binder.clearCallingIdentity();
    String table = DbAdapter.myDbHelper.TABLE_WORKOUT;
    String[] columns = null;
    String selection = null;
    String[] selectionArgs = null;
    String groupBy = null;
    String having = null;
    String orderBy = DbAdapter.myDbHelper.COL_WORKOUT_ID + " DESC ";
    String limit = "10";

    dbWorkout = new DbWorkout();
    cursor = helper.myhelper.getReadableDatabase().query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
    while(cursor.moveToNext())
    {
        dbWorkout.setId(cursor.getInt(cursor.getColumnIndex(DbAdapter.myDbHelper.COL_WORKOUT_ID)));
        dbWorkout.setName(cursor.getString(cursor.getColumnIndex(DbAdapter.myDbHelper.COL_WORKOUT_NAME)));
        dbWorkout.setDate(cursor.getString(cursor.getColumnIndex(DbAdapter.myDbHelper.COL_WORKOUT_DATE)));
    }

    Binder.restoreCallingIdentity(identityToken);
}

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

@Override
public void onDataSetChanged() {
    // Listen for data changes and initialize the cursor again
    initCursor();
}

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

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

@Override
public RemoteViews getViewAt(int i) {
    // Populate the widget's single list item
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.list_item_widget);
    cursor.moveToPosition(i);
    remoteViews.setTextViewText(R.id.text_view_widget_count, String.valueOf(i+1));
    remoteViews.setTextViewText(R.id.text_view_widget_name, cursor.getString(cursor.getColumnIndex(DbAdapter.myDbHelper.COL_WORKOUT_NAME)));
    remoteViews.setTextViewText(R.id.text_view_widget_date,cursor.getString(cursor.getColumnIndex(DbAdapter.myDbHelper.COL_WORKOUT_DATE)));

    Bundle extras = new Bundle();
    extras.putInt(WorkoutWidgetProvider.EXTRA_ITEM, dbWorkout.getId());
    Intent fillInIntent = new Intent();
    fillInIntent.putExtras(extras);
    // Make it possible to distinguish the individual on-click
    // action of a given item
    remoteViews.setOnClickFillInIntent(R.id.item_frame, fillInIntent);
    return remoteViews;
}

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

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

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

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

}

0 个答案:

没有答案
相关问题