【问题标题】:Room Insert only inserts the primary key and not columnsRoom Insert 只插入主键,不插入列
【发布时间】:2018-08-21 15:13:10
【问题描述】:

我正在尝试将一个对象插入我的房间数据库,但似乎只插入了主键。当我尝试访问它们时,附加的字符串列表和整数列表都为空。

实体

@Entity
public class DayMenu{
@PrimaryKey @NonNull
private String dayDate;


@TypeConverters(IntegerConverter.class)
private List<Integer> dayRecipesID;

@TypeConverters(StringConverter.class)
private List<String> dayRecipesTitle;

@Dao
public interface DayMenuDao {

@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertDayMenu(DayMenu dayMenu);

@Query("select * from DayMenu")
List<DayMenu> getAll();

@Query("select * from DayMenu")
LiveData<List<DayMenu>> getAllLD();

@Query("select dayDate from DayMenu where DayMenu.dayDate = :day_date")
DayMenu getFromDate(String day_date);
}

当我查看 dayMenu(如下)时,在我发送它以插入之前,它将有两个具有正确数据的列表。但是,当我调用 getAll()、getAllLD() 或 getFromDate() 时,这些信息要么没有插入,要么没有被恢复。

主活动

        btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (etDate.getText().length() != 0) {
                String currentDate = etDate.getText().toString();

                DayMenu dayMenu = new DayMenu();
                dayMenu.setDayDate(currentDate);

                List<Integer> recIDs = new ArrayList<>();
                List<String> recTits = new ArrayList<>();
                StringBuilder sb = new StringBuilder();
                int childCount = linearLayoutParent.getChildCount();
                if (childCount > 1) {
                    for (int i = 0; i < childCount - 1; i++) {
                        View linearLayoutChild = linearLayoutParent.getChildAt(i);
                        ViewGroup lLC = (ViewGroup) linearLayoutChild;
                        Button recipeButton = (Button) lLC.getChildAt(0);
                        recIDs.add(Integer.parseInt(recipeButton.getTag().toString()));
                        recTits.add(recipeButton.getText().toString());

                    }

                    dayMenu.setDayRecipesID(recIDs);
                    dayMenu.setDayRecipesTitle(recTits);

                    for(String str : dayMenu.getDayRecipesTitle()){
                        sb.append(str).append(", ");
                    }

new AsyncIns(mViewModel.GetDayMenuDao(),dayMenu).execute();

                    Toast.makeText(_context, sb,Toast.LENGTH_SHORT).show();
                }
                else{
                    NoRecipesAddedHandler(_context);
                }
            }
            else{
                NoDateSelectedHandler(_context);
            }
        }
    });

异步任务

    private class AsyncIns extends AsyncTask<Void, Void, DayMenu>{
    final DayMenuDao mDao;
    final DayMenu mDM;

    AsyncIns(DayMenuDao dao, DayMenu dm){
        mDao = dao;
        mDM = dm;
    }

    @Override
    protected DayMenu doInBackground(Void... params){
        mDao.insertDayMenu(mDM);
        return mDao.getFromDate(mDM.getDayDate());
    }

    @Override
    protected void onPostExecute(DayMenu d){
        Toast.makeText(_context,d.getDayDate(),Toast.LENGTH_LONG).show();
    }
}

这将显示保存到数据库的日期,我已经能够使用其他方法查看日期。但是,当我尝试访问 recipeIds 和标题时,列表为空。

我不太确定出了什么问题,insertDayMenu 似乎只是插入了 Entities 主键。

【问题讨论】:

    标签: java android android-room android-architecture-components


    【解决方案1】:

    SQLite 不能具有数据类型 List。 也许您想要这些领域的一对多关系?一个 DayMenu 可以有多个 DayRecipes。

    【讨论】:

      【解决方案2】:

      您可以使用 Stetho 库查看数据库中的实际内容:

      将此行添加到您的 build.gradule 模块文件中:

          implementation 'com.facebook.stetho:stetho:1.5.0'
      

      然后像这样添加一个 Application.java 文件:

      public class MyApplication extends Application {
          public void onCreate() {
              super.onCreate();
              Context context = getBaseContext();
      
              Stetho.initializeWithDefaults(this);
      
          }
      }
      

      然后在manifest文件中添加:在application标签中添加:

      android:name="MyApplication"
      

      当你的应用运行时打开谷歌浏览器并输入:

      chrome://inspect/#devices
      

      您应该看到您的应用 - 检查它,您将看到数据库中的内容

      另外我刚刚注意到你的名字使用驼峰式,这对 Java 非常好,但对数据库却不是:

      您的代码需要如下所示:

      @TypeConverters(IntegerConverter.class)
      @SerializedName("day_recipes_id")
      @Expose
      private List<Integer> dayRecipesID;
      
      @TypeConverters(StringConverter.class)
      @SerializedName("day_recipes_title")
      @Expose
      private List<Integer> dayRecipesTitle;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-05
        • 1970-01-01
        • 2019-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多