简体   繁体   中英

How do I create a new instance of a database using Room?

My app is analogous to a recipe app. I want to create a list of recipes that are seen when the user opens the app, which can be clicked on, and when clicked on a new list is created which shows the steps to make the recipe.

I am using the Room persistence library to do this and the app is working perfectly up to the 'seeing a list of recipes' and these can be clicked on. Currently, when a recipe is clicked on, all that happens is the user is redirected to the original list of recipes again (ie the activity they were already on). How do I create a new instance of the database that I can populate with the recipe ingredients when a user clicks on the recipe?

I'm not sure on what the correct phrasing for what I want is - do I need a new instance of the database? Do I need a second entity in the Room database? Do I need to make a second DAO and ViewModel for the 'steps in the recipe'?

adapter.setOnRecipeClickListener(new RecipeAdapter.OnRecipeClickListener() {
    @Override
    public void onRecipeClick(Recipe recipe) {
        Intent intent = new Intent(GroupRecipesActivity.this, 
        SpecificRecipeActivity.class);
        intent.putExtra(AddEditRecipeActivity.EXTRA_RECIPE_ID, 
        recipe.getId());
        intent.putExtra(AddEditRecipeActivity.EXTRA_RECIPE_NAME, 
        recipe.getRecipeName());
        startActivityForResult(intent, SPECIFIC_RECIPE_REQUEST);

This is the new class where I want to have a 'refreshed' (?new table) database where I can put in the specific recipe instructions:

package com.oscebosskey.obkm6;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import java.util.List;

public class SpecificRecipeActivity extends AppCompatActivity {
    private RecipeViewModel recipeViewModel;

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

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);

        final RecipeAdapter adapter = new RecipeAdapter();
        recyclerView.setAdapter(adapter);

        recipeViewModel = 
        ViewModelProviders.of(this).get(RecipeViewModel.class); 
        recipeViewModel.getAllRecipes().observe(this, new 
        Observer<List<Recipe>>() {
            @Override
            public void onChanged(List<Recipe> recipes) {
                adapter.submitList(recipes);
            }
        });
    }
}

I think this issue is due to a lack of familiarity with the nomenclature for Room databases - please forgive this newbie!

With the code you have, it seems you are trying to update the recycler view when you add a new recipe.

I assume your viewModel does something like this:

var allRecipes  : LiveData<List<Recipes>> = repository.getAllRecipes()

Where repository:

class MessagesRepository(private val recipeDAO: recipeDAO) {
   fun getAllRecipes() : LiveData<List<Recipe>> {
       return messagesDAO.getAllRecipes()
   }
}

And DAO:

@Dao  
interface RecipesDAO {
  @Query("Select * From Recipes")
  fun getAllRecipes() : LiveData<List<Recipes>>
}

And in your RecipeAdapter:

class RecipeAdapter internal constructor(val context: Context?, recipeList : ArrayList<Recipes>) : RecyclerView.Adapter<ContactsRecyclerAdapter.ContactsViewHolder>() {
    fun setRecipeList(recipeList : List<Recipes>) {
      this.emailListSet = ArrayList(emailListSet)
      notifyDataSetChanged()
    }
}

Then you would need to change to:

 recipeViewModel.getAllRecipes().observe(this, new 
    Observer<List<Recipe>>() {
        @Override
        public void onChanged(List<Recipe> recipes) {
            adapter.setRecipeList(recipes);
            adapter.notifyDatasetChanges();
        }
    });

To update a recipe list I would assume that the user would be clicking on an item in your recycler view, then you would show a new fragment or activity that would allow them to update the recipe. However, updating the recipe will not update your recycler view. You will need to observe the contents of the recipe table and then redraw.

You do not need a new DAO, you can use the existing one. It would be nice to see more of your room entities code, though.

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