首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >具有不同ListView数据的相同小部件

具有不同ListView数据的相同小部件
EN

Stack Overflow用户
提问于 2018-06-23 01:10:53
回答 1查看 370关注 0票数 2

我有一个具有ListView的简单AppWidget。

该小部件具有简单配置活动,您可以在其中选择要显示的数据集。该信息(int )存储在SharedPreferences中,稍后从RemoteViewsFactory中的SP获取。

一切都很完美,直到我在屏幕上添加了另一个Widget。如果被选中,新的Widget不会加载不同的数据,而是从以前的Widget中获取数据。

通过少量的调试,我认为我的onUpdate方法没有被调用,但我在这里找不到问题所在。

WidgetConfig.java

代码语言:javascript
复制
public class IngredientsWidgetConfigureActivity extends Activity implements RecipesAdapter.RecipesAdapterOnClickHandler {


int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
RecipesAdapter mAdapter;


public IngredientsWidgetConfigureActivity(){
    super();
}

//save recipeId into SharedPrefs
static void saveSelectedRecipe (Context context, int appWidgetId, int selectedRecipe){
    SharedPreferences.Editor prefs = context.getSharedPreferences(AppConstants.PREFS_NAME, 0).edit();
    prefs.putInt(AppConstants.PREF_PREFIX_KEY + appWidgetId, selectedRecipe);
    prefs.apply();
}


@Override
protected void onCreate(@Nullable Bundle icicle) {
    super.onCreate(icicle);

    setResult(RESULT_CANCELED);

    setContentView(R.layout.ingredients_widget_configure);

    //setup RecyclerView
    ...

    Intent intent = getIntent();
    Bundle extras = intent.getExtras();

    if (extras != null){
        mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
    }
    if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID){
        finish();
        return;
    }

}

@Override
public void onClick(Recipe recipe) {
    final Context context = IngredientsWidgetConfigureActivity.this;

    // When the button is clicked, store the recipe Id
    int selectedRecipe = recipe.id;
    saveSelectedRecipe(context, mAppWidgetId, selectedRecipe);

    // It is the responsibility of the configuration activity to update the app widget
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    IngredientsWidget.updateAppWidget(context, appWidgetManager, mAppWidgetId);

    // Make sure we pass back the original appWidgetId
    Intent resultValue = new Intent();
    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
    setResult(RESULT_OK, resultValue);
    finish();
}

WidgetProvider

代码语言:javascript
复制
....  
static void updateAppWidget(final Context context, AppWidgetManager appWidgetManager,
                     int appWidgetId) {

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.ingredients_widget_layout);

    Intent intent = new Intent(context, WidgetRemoteViewsService.class);
    intent.putExtra(AppConstants.APP_WIDGET_ID_KEY, appWidgetId);
    views.setRemoteAdapter(R.id.widget_list_view, intent);


    //TODO: recipes are not changing in different widgets

    // 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);


    }

}

最后是我的

RemoteViewsFactory.java

代码语言:javascript
复制
public WidgetRemoteViewsFactory (Context context, Intent intent){
    mContext = context;
    appWidgetId = intent.getIntExtra(AppConstants.APP_WIDGET_ID_KEY, 1);
}

@Override
public void onCreate() {

}

@Override
public void onDataSetChanged() {

    final long identityToken = Binder.clearCallingIdentity();

//get id from SharedPref
    RecipeDatabase mDb = RecipeDatabase.getInstance(mContext);
    SharedPreferences prefs = mContext.getSharedPreferences(AppConstants.PREFS_NAME, 0);
    recipeId = prefs.getInt(AppConstants.PREF_PREFIX_KEY + appWidgetId, 9999);


    recipe = mDb.recipeDAO().loadRecipe(recipeId);


    Binder.restoreCallingIdentity(identityToken);


}

@Override
public void onDestroy() {

}

@Override
public int getCount() {
    if (recipe == null) {
        return 0;
    } else {
        return recipe.ingredients.size();
    }
}

@Override
public RemoteViews getViewAt(int position) {
    if (position > recipe.ingredients.size()){
        return null;
    }


    RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.widget_list_item);
    remoteViews.setTextViewText(R.id.widget_text, recipe.ingredients.get(position).getIngredient());

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

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-23 06:27:28

解决..。我必须将新数据放入updateAppWidget中的Intent中,以便工厂加载新实例,而不是重用新实例。

所以我用每个小工具发送新的随机值数据,这样它就会正确更新。

updateAppWidget代码片段:

代码语言:javascript
复制
//send intent and data to Adapter
    Intent intent = new Intent(context, WidgetRemoteViewsService.class);
    intent.putExtra(AppConstants.APP_WIDGET_ID_KEY, appWidgetId);

    //set random data to initialize new Factory
    Random rnd = new Random();
    intent.setData(Uri.fromParts("content", String.valueOf(rnd.nextInt()), null));

    views.setRemoteAdapter(R.id.widget_list_view, intent);

感谢@TheFedex87

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50992720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档