简体   繁体   中英

Android: Passing variables that allows recyclerview to be populated by specific strings

I'm trying to create a code saying "Where the username that is logged in matches the username of a created recipe, only show these database entries in the recyclerview on my MyRecipes.java activity". I'm having trouble working out where to put this potential statement. Looking at my code, where would you put that statement, if or otherwise?

The userLoggedIn and loggedUser is the variable for the currently logged in user.

The thisUser is what I've set as the users pulled from the database when the recyclerview is populating in the recyclerview.java class.

Any help would be greatly appreciated!

RecyclerView.java class

    private static String thisUser;
    String userLoggedIn = HomeActivity.getUserLogged();
    private Context mContext;
    private RecipesAdapter mRecipeAdapter;

    public void setConfig (RecyclerView recyclerView, Context context, List<Recipes> recipes, List<String> keys){

        mContext = context;
        mRecipeAdapter = new RecipesAdapter(recipes, keys);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(mRecipeAdapter);
    }

    class RecipeItemView extends RecyclerView.ViewHolder{
        private TextView mTitle;
        private TextView mIngredients;
        private TextView mMethod;
        private TextView mUser;
        private String key;

        public RecipeItemView(ViewGroup parent){
            super(LayoutInflater.from(mContext).inflate(R.layout.recipe_list_item, parent,false));

            mTitle = (TextView) itemView.findViewById(R.id.tvTitle);
            mMethod = (TextView) itemView.findViewById(R.id.tvMethod);
            mIngredients = (TextView) itemView.findViewById(R.id.tvIngredients);
            mUser = (TextView) itemView.findViewById(R.id.tvUser);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, RecipeDetails.class);
                    intent.putExtra("key", key);
                    intent.putExtra("title", mTitle.getText().toString());
                    intent.putExtra("ingredients", mIngredients.getText().toString());
                    intent.putExtra("method", mMethod.getText().toString());

                    mContext.startActivity(intent);
                }
            });
        }

        public void bind(Recipes recipes, String key) {

                mTitle.setText(recipes.getTitle());
                mIngredients.setText(recipes.getIngredients());
                mMethod.setText(recipes.getMethod());
                mUser.setText(recipes.getCreatedUser());
                thisUser = mUser.getText().toString().trim();
                this.key = key;
            }
    }

    public static String getUserLoggedIn(){
        return thisUser;
    }

    class RecipesAdapter extends RecyclerView.Adapter<RecipeItemView> {
        private List<Recipes> mRecipeList;
        private List<String> mKeys;

        public RecipesAdapter(List<Recipes> mRecipeList, List<String> mKeys) {
            this.mRecipeList = mRecipeList;
            this.mKeys = mKeys;
        }

        @NonNull
        @Override
        public RecipeItemView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new RecipeItemView(parent);
        }

        @Override
        public void onBindViewHolder(@NonNull RecipeItemView holder, int position) {
            holder.bind(mRecipeList.get(position), mKeys.get(position));
        }

        @Override
        public int getItemCount() {
            return mRecipeList.size();
        }
    }
}

MyRecipes.java (Where the recycler view populates and shows all the recipes)

    String loggedUser = HomeActivity.getUserLogged();
    String thisUser = RecyclerViewConfig.getUserLoggedIn();
    Button addRecipes;
    private RecyclerView mRecyclerView;
    private String passedUsername;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_recipes);

        passedUsername = getIntent().getStringExtra("loggedUsername1");
        mRecyclerView = (RecyclerView) findViewById(R.id.rvRecipes);
        new FirebaseDatabaseHelper().readRecipes(new FirebaseDatabaseHelper.DataStatus() {
            @Override
            public void DataIsLoaded(List<Recipes> recipes, List<String> keys) {
                    new RecyclerViewConfig().setConfig(mRecyclerView, MyRecipesActivity.this, recipes, keys);
                }

            @Override
            public void DataIsInserted() {
            }

            @Override
            public void DataIsUpdated() {
            }

            @Override
            public void DataIsDeleted() {
            }
        });

        addRecipes = findViewById(R.id.btnAddNewRecipe);
        addRecipes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent addrecipe = new Intent(MyRecipesActivity.this, AddRecipes.class);
                addrecipe.putExtra("loggedUsername2", passedUsername);
                startActivity(addrecipe);
            }
        });
    }
}

If i understand correctly, you need to show the recipes to the user only if he/she is the one who created it? Assuming that, below change in the code will do the trick.

Change your bind() method as follows.

public void bind(Recipes recipes, String key) {
        thisUser = mUser.getText().toString().trim();
        if(thisUser.equals(recipes.getCreatedUser)) {
           mTitle.setText(recipes.getTitle());
           mIngredients.setText(recipes.getIngredients());
           mMethod.setText(recipes.getMethod());
           mUser.setText(recipes.getCreatedUser());
           this.key = key;
        }
}

Though that is a naive approach, a better approach would be to filter the recipes list with created user when you load data from Firebase with readRecipes() method itself. To do that a query can be used as below(the exact query below might not work for you depending on your DB structure. Change it as needed)

Query query = reference.child("Recipes").orderByChild("userCreated").equalTo(thisUser);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM