我试图将一个对象插入到房间数据库中,但是似乎只插入了主键。当我尝试访问其他字符串列表和整数列表时,它们都为null。实体@Entitypublic class DayMenu{@PrimaryKey @NonNullprivate String dayDate;@TypeConverters(IntegerConverter.class)private List<Integer> dayRecipesID;@TypeConverters(StringConverter.class)private List<String> dayRecipesTitle;道@Daopublic 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(); }}这将显示保存到数据库的日期,并且我已经能够使用其他方法看到日期。但是,当我尝试访问配方ID和标题时,列表为空。我不太确定出了什么问题,insertDayMenu似乎只插入Entities主键。 最佳答案 SQLite不能具有数据类型List 。也许您想要这些领域的一对多关系?一个DayMenu可以有多个DayRecipes。 10-07 15:32