AppWidget AdapterViewFlipper not working on item click

I created a simple appwidget with AdapterViewFlipper. The data display and flip actions work correctly, but if I want to handle the click element's action it doesn't work. But if I change the AdapterViewFlipper to ListView in the xml layout, the click element works correctly.

widget provider class

public class KidChildrenAdaperViewFlipperWidget extends AppWidgetProvider {

public static final String WIDGET_ACTION = "WIDGET_ACTION";
public static final String ACTION_NEXT = ".ACTION_NEXT";
public static final String ACTION_PREVIOUS = "ACTION_PREVIOUS";
private static final String ACTION = "ACTION";

public static final String TAG = "KidChildrenListWidget";

@Override
public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();
    Bundle extras = intent.getExtras();
    String actionButton = (String) (extras == null ? null : extras.get(ACTION));

    if (action.equals(KidChildrenAdaperViewFlipperWidget.WIDGET_ACTION)){
        Toast.makeText(context, "Kliknięto: ", Toast.LENGTH_SHORT).show();
    }

    if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action) && actionButton != null) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_view_flipper_children);
        switch (actionButton) {
            case ACTION_NEXT: {
                remoteViews.showNext(R.id.advKidWidgetChildren);
            }
            break;

            case ACTION_PREVIOUS: {
                remoteViews.showPrevious(R.id.advKidWidgetChildren);
            }
            break;
        }

        int widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
        AppWidgetManager.getInstance(context).updateAppWidget(widgetId, remoteViews);
    }else {
        super.onReceive(context, intent);
    }
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    for (int i = 0; i < appWidgetIds.length; i++) {

        Intent widgetIntent = new Intent(context, WidgetChildAdapterViewFlipperService.class);
        widgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        widgetIntent.setData(Uri.parse(widgetIntent.toUri(Intent.URI_INTENT_SCHEME)));

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_view_flipper_children);

        remoteViews.setRemoteAdapter(R.id.advKidWidgetChildren, widgetIntent);

        remoteViews.setEmptyView(R.id.advKidWidgetChildren, R.id.tvKidWidgetEmptyView);


        Intent activityIntent = new Intent(context, KidChildrenAdaperViewFlipperWidget.class);
        // Set the action for the intent.
        // When the user touches a particular view, it will have the effect of
        // broadcasting WIDGET_ACTION.
        activityIntent.setAction(WIDGET_ACTION);
        activityIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        widgetIntent.setData(Uri.parse(widgetIntent.toUri(Intent.URI_INTENT_SCHEME)));
        PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, activityIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setPendingIntentTemplate(R.id.advKidWidgetChildren, toastPendingIntent);



        Intent previousIntent = new Intent(context, KidChildrenAdaperViewFlipperWidget.class);
        previousIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        previousIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        previousIntent.putExtra(ACTION, ACTION_PREVIOUS);
        PendingIntent previousPendingIntent = PendingIntent.getBroadcast(context, 1, previousIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.ivWVFCPrevious, previousPendingIntent);


        Intent nextIntent = new Intent(context, KidChildrenAdaperViewFlipperWidget.class);
        nextIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        nextIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        nextIntent.putExtra(ACTION, ACTION_NEXT);
        PendingIntent nextPendingIntent = PendingIntent.getBroadcast(context, 2, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.ivWVFCNext, nextPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);

    }

    super.onUpdate(context, appWidgetManager, appWidgetIds);
    Log.d(TAG, "Zakutalizowano widget");
}

      

}

Class with a widget service

public class WidgetChildAdapterViewFlipperService extends RemoteViewsService {

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

public class WidgetFactoryADV implements RemoteViewsFactory {


    private Context context;
    private int appWidgetId;

    private KidDatabaseManager kidDatabaseManager;
    private List<ChildObject> childObjectList;

    public WidgetFactoryADV(Context context, Intent intent ) {
        this.context = context;
        appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);

    }

    @Override
    public void onCreate() {

        kidDatabaseManager = new KidDatabaseManager(context);
        childObjectList = kidDatabaseManager.getChildren(((ApplicationController) context.getApplicationContext()).getUser(context).getId());
    }

    @Override
    public void onDataSetChanged() {
        childObjectList = kidDatabaseManager.getChildren(((ApplicationController) context.getApplicationContext()).getUser(context).getId());

        for (int i=0; i<childObjectList.size(); i++){
            getViewAt(i);
        }
    }

    @Override
    public void onDestroy() {
    }

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

    @Override
    public RemoteViews getViewAt(int i) {

        ChildObject childObject = childObjectList.get(i);

        RemoteViews widgetRow = new RemoteViews(context.getPackageName(), R.layout.widget_children_adv_item);

        widgetRow.setTextViewText(R.id.tvWidgetItemChildName, childObject.getName());

        String sdf = new SimpleDateFormat("yy.MM.dd HH:mm:ss").format(new Date(childObject.getPositionCreatedAt() * 1000));
        widgetRow.setTextViewText(R.id.tvWidgetItemChildPositionTime, Html.fromHtml(String.format(getString(R.string.last_position),
                sdf)));

        Address address = Utils.getAddress(context, childObject.getLatitude(), childObject.getLongitude());
        if (address != null) {
            String thoroughfare = address.getThoroughfare() == null ? "" : address.getThoroughfare();
            String localiyty = address.getLocality() == null ? "" : (", " + address.getLocality());
            String featureName = address.getFeatureName() == null ? "" : (" " + address.getFeatureName());


            widgetRow.setTextViewText(R.id.tvWidgetItemAddress, thoroughfare + featureName + localiyty);
        }

        widgetRow.setTextViewText(R.id.tvWidgetItemAccuracy, ChildUtil.getAccuracyIndicator(context, childObject.getAccuracy()));

        ChildUtil.setActivityIconForRemoteView(context, widgetRow, childObject.getActivity());


        widgetRow.setTextViewText(R.id.tvWidgetItemBattery, childObject.getBattery() + " %");

        Intent widgetIntent = new Intent();
        Bundle widgetBundle = new Bundle();

        widgetBundle.putInt("index", i);
        widgetIntent.putExtras(widgetBundle);

        widgetRow.setOnClickFillInIntent(R.id.rlWidgetADVItem, widgetIntent);


        return widgetRow;
    }

    @Override
    public RemoteViews getLoadingView() {

        return null;
    }

    @Override
    public int getViewTypeCount() {
        return childObjectList.size();
    }

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

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

      

}

I've spent a lot of time solving this problem, but I don't know why it doesn't work.

Thanks for any suggestion that might help me with this problem

+3


source to share





All Articles