在小部件上的ListView中显示SharedPreference项目

时间:2018-08-15 02:08:12

标签: android listview widget sharedpreferences

我有一份要存储在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);

    }

1 个答案:

答案 0 :(得分:1)

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