Активность открытия списка виджетов при нажатии
Я много искал на этом веб-сайте, чтобы помочь мне разобраться в этом, но ни один из вопросов мне не помог. Я использовал пример виджета стека, чтобы начать работу, но я не могу получить элемент списка при щелчке, чтобы открыть действие. Я хочу отправить 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);
}
}
class 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;
}
}