0

我的应用程序有一个主屏幕小部件,但无法单击列表中的任何项目。当我单击任何项​​目时,没有任何反应。任何帮助都受到高度赞赏。

WidgetProvider.class

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.RemoteViews;

import com.revodroid.notes.notes.Adapter.DatabaseHelper;
import com.revodroid.notes.notes.R;

public class WidgetProvider extends AppWidgetProvider {

public static final String DATA_FETCHED = "com.revodroid.notes.notes.Widget.WidgetProvider";
public static final String ACTION_NOTIFY = "com.revodroid.notes.notes.ACTION_NOTIFY_DATASET_CHANGED";
DatabaseHelper databaseHelper;
public static final String TOAST_ACTION = "com.revodroid.notes.notes.Widget.TOAST_ACTION";
public static final String EXTRA_ITEM = "com.revodroid.notes.notes.Widget.EXTRA_ITEM";
private static final String EXTRA_NOTE = "EXTRA_NOTE";

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    databaseHelper = new DatabaseHelper(context);
    for (int i = 0; i < appWidgetIds.length; i++) {
        RemoteViews remoteViews = updateWidgetListView(context, appWidgetIds[i]);

        ComponentName component=new ComponentName(context,WidgetProvider.class);
        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listViewWidget);
        appWidgetManager.updateAppWidget(component, remoteViews);

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

        Intent toastIntent = new Intent(context, ViewNoteCollapseActivity.class);
        toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        toastIntent.putExtra(EXTRA_NOTE, appWidgetIds[i]);

        PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, appWidgetIds[i], toastIntent,PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setPendingIntentTemplate(R.id.listViewWidget, toastPendingIntent);

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

    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}


private static int[] getCurrentWidgetIds(Context context){
    return AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, WidgetProvider.class));
}

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    if(ACTION_NOTIFY.equals(intent.getAction())){
        int[] widgetIds = getCurrentWidgetIds(context);
        if(widgetIds.length>0){
            AppWidgetManager.getInstance(context).notifyAppWidgetViewDataChanged(widgetIds, R.id.listViewWidget);
        }
    }

    AppWidgetManager mgr = AppWidgetManager.getInstance(context);
    if (intent.getAction().equals(TOAST_ACTION)) {
        int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        int viewIndex = intent.getIntExtra(EXTRA_ITEM, 0);
        Toast.makeText(context, "Touched view " + viewIndex, Toast.LENGTH_SHORT).show();
    }

    if (intent.getAction().equals(DATA_FETCHED)) {
        int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        RemoteViews remoteViews = updateWidgetListView(context, appWidgetId);
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    }
}

private RemoteViews updateWidgetListView(Context context, int appWidgetId) {
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
    Intent svcIntent = new Intent(context, WidgetService.class);
    svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
    remoteViews.setRemoteAdapter(appWidgetId, R.id.listViewWidget, svcIntent);
    remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view);
    return remoteViews;
}
}

WidgetDataProvider.class

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService.RemoteViewsFactory;

import com.revodroid.notes.notes.Activity.ViewNoteCollapseActivity;
import com.revodroid.notes.notes.Adapter.DatabaseHelper;
import com.revodroid.notes.notes.Adapter.Note;
import com.revodroid.notes.notes.R;
import com.revodroid.notes.notes.Util.RequestResultCode;

import java.util.List;

public class WidgetDataProvider implements RemoteViewsFactory {
private Context context = null;
private int appWidgetId;

List<Note> notes;
DatabaseHelper databaseHelper;

String STAGGER_CONTENT = "EXTRA CONTENT";
private static final String EXTRA_NOTE = "EXTRA_NOTE";

AppWidgetManager appWidgetManager ;

public WidgetDataProvider(Context context, Intent intent) {
    this.context = context;
    appWidgetId =      intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
    appWidgetManager = AppWidgetManager.getInstance(context);
}

@Override
public void onCreate() {
    databaseHelper = new DatabaseHelper(context);
    notes = databaseHelper.getAllNotes();
}

@Override
public void onDataSetChanged() {
    Log.v("Database Get All Notes","Get All Notes");
    appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.listViewWidget);
}

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

@Override
public long getItemId(int position) {
    return notes.get(position).getId();
}

@Override
public RemoteViews getViewAt(int position) {
    final RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget_row);

    Note note = notes.get(position);

    remoteView.setTextViewText(R.id.heading, note.getTitle());
    remoteView.setTextViewText(R.id.content, note.getContent());

    /*Intent intent = new Intent(getApplicationContext(), ViewNoteCollapseActivity.class);
     intent.putExtra(EXTRA_NOTE, note1);
     setResult(RequestResultCode.REQUEST_CODE_VIEW_NOTE, intent);*/

    Bundle extras = new Bundle();
    extras.putInt(WidgetProvider.EXTRA_ITEM, position);
    extras.putSerializable(EXTRA_NOTE, note);
    Intent fillInIntent = new Intent();
    fillInIntent.putExtras(extras);
    remoteView.setOnClickFillInIntent(R.id.listViewWidget, fillInIntent);

    return remoteView;
}

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

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

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


@Override
public void onDestroy() {
}

}
4

0 回答 0