简体   繁体   English

如何使用Room创建数据库的新实例?

[英]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. 我正在使用Room Persistence库来执行此操作,并且该应用程序在“看到食谱列表”之前运行良好,可以单击这些。 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? 我需要在Room数据库中添加第二个实体吗? Do I need to make a second DAO and ViewModel for the 'steps in the recipe'? 我是否需要为“配方中的步骤”制作第二个DAO和ViewModel?

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! 我认为此问题是由于对Room数据库的命名法不熟悉引起的-请原谅这个新手!

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: 我假设您的viewModel会执行以下操作:

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:

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

And in your RecipeAdapter: 在您的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. 您不需要新的DAO,可以使用现有的DAO。 It would be nice to see more of your room entities code, though. 不过,很高兴看到您的更多房间实体代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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