【问题标题】:Dispaly SharedPreference Items in ListView on Widget在 Widget 的 ListView 中显示共享首选项
【发布时间】:2019-01-21 22:27:49
【问题描述】:

我有一个存储在SharedPreferences 中并传递给我的小部件的成分列表。下面是我在单击RecyclerView 中的相应成分后将它们存储到 SharedPreferences 中的位置:

  @Override
    public void onBindViewHolder(@NonNull MyAdapter.ViewHolder holder, final int position) {
        String id = mRecipeDataSet.get(position).getId();
        final String recipeName = mRecipeDataSet.get(position).getName();
        final List<Ingredients> ingredients = mRecipeDataSet.get(position).getIngredients();
        holder.mTextView.setText(recipeName);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //STORING INGREDIENTS TO SHAREPREFERNCES
                SharedPreferences.Editor editor = mPrefs.edit();
                for (Ingredients ingredients : ingredients){
                    editor.putString(ingredients.getIngredient(), ingredients.getIngredient());
                    editor.putString(ingredients.getIngredient(), ingredients.getMeasure());
                    editor.putString(ingredients.getIngredient(), ingredients.getQuantity());
                }
                   editor.apply();


            }
        });

    }

下面是我的 AppWidgetProvider,我试图从首选项中检索项目并通过我的小部件上的 ListView 显示。我正在为 RemoteViewService 的概念苦苦挣扎,因为与设置 ListView 适配器并相应地显示项目的标准方法相比,它似乎过于复杂:

public class BakingWidgetProvider extends AppWidgetProvider {

    private static final String RECIPE_NAME = "RECIPE_NAME";
    //TODO: Another method of getting information from Activity/Fragment/POJO to widget - unclear why I would need to do this but also added this string to my manifest
    public static final String ACTION_TEXT_CHANGED = "kitchenpal.troychuinard.com.kitchenpal.TEXT_CHANGED";
    private static final String PREFS = "PREFS";
    private static final String INGREDIENTS = "INGREDIENTS";
    private static String mRecipeName;
    private static String mRecipeIngredients;

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

        SharedPreferences preferences = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE);
        mRecipeName = preferences.getString(RECIPE_NAME,null);
        mRecipeIngredients = preferences.getString(INGREDIENTS, null);
        Log.v(INGREDIENTS, mRecipeIngredients);
        CharSequence widgetText = context.getString(R.string.appwidget_text);
        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.baking_widget_provider);
        //TODO: I can successfully display the ingredients, however I do not know how to organize into a ListView
        views.setTextViewText(R.id.selected_recipe, mRecipeIngredients);

        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);

        views.setOnClickPendingIntent(R.id.chef, pIntent);
        appWidgetManager.updateAppWidget(appWidgetId, views);

    }

【问题讨论】:

    标签: android listview widget sharedpreferences


    【解决方案1】:

    首先,您需要在onBindViewHolder(...) 方法中使用editor.apply(); 将更改应用到SharedPreferences

    【讨论】:

    • 这与提出的问题完全无关,即询问如何在 App Widget ListView 中显示现有数据。
    猜你喜欢
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 2012-09-08
    相关资源
    最近更新 更多