我想构建一个Deckbuilder,该Deckbuilder允许您将创建的套牌保存在设备本地。
Decklist存储在名为TransferDeck
的Arraylist中。我想存储在房间数据库中。 我的问题是,我不知道该如何正确地填充数据库,以及数据是否来自Arraylist。
我习惯于使用Arraylist,在下面您会看到我尝试存储数据的信息:
这就是我尝试过的方法,但可惜的是不起作用:
private void populateDB(final List<TransferDeck> mTransferDeck) {
new Thread(new Runnable() {
@Override
public void run() {
List<SaveDeck> mSaveDeck = new ArrayList<>();
for(int i = 0; i<mTransferDeck.size(); i++){
mSaveDeck.add(new SaveDeck(i, "FirstSavedDeck", mTransferDeck.get(i).getCardImage() ,mTransferDeck.get(i).getTypeImage(), mTransferDeck.get(i).getCost(), mTransferDeck.get(i).getName(), mTransferDeck.get(i).getNumber()));
}
mSavedDecksDB.deckBuilderDao().insertCards(mSaveDeck);
}
}).start();
}
下面您可以找到我的其余代码,但是上面的代码应该足以弄清楚我想做什么...
我创建了SaveDeck类,该类应该能够保存具有给定Deckname的Deck : :-
@实体 公共类SaveDeck实现了Serializable { @PrimaryKey(autoGenerate = true) private int _id;
public SaveDeck(int _id, String deckName, int cardImage, int typeImage, Integer cardCost, String cardName, Integer cardNumber) {
this._id = _id;
DeckName = deckName;
CardImage = cardImage;
TypeImage = typeImage;
CardCost = cardCost;
CardName = cardName;
CardNumber = cardNumber;
}
@ColumnInfo(name = "DeckName")
private String DeckName;
@ColumnInfo(name = "CardImage")
private int CardImage;
@ColumnInfo(name = "TypeImage")
private int TypeImage;
@ColumnInfo(name = "CardCost")
private Integer CardCost;
@ColumnInfo(name = "CardName")
private String CardName;
@ColumnInfo(name = "CardNumber")
private Integer CardNumber;
}
我创建了Dao类,如下所示: :-
@道 公共接口DeckBuilderDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
public long[] insertCards(SaveDeck... saveDecks);
@Insert(onConflict = OnConflictStrategy.IGNORE)
public long insertCard(SaveDeck saveDecks);
@Update
public int updateCardBaseEntries(SaveDeck... saveDecks);
@Update
public int updateCardBaseEntry(SaveDeck saveDecks);
@Delete
public int deleteCardBaseEntried(SaveDeck... saveDecks);
@Delete
public int deleteCardBaseEntry(SaveDeck saveDecks);
@Query("SELECT * FROM SaveDeck")
public SaveDeck[] getAllDecks();
//probably I do not need the getAllDecks Query. Right now I only need the following one:
@Query("SELECT * FROM SaveDeck WHERE DeckName = :NameOfDeck ORDER BY DeckName, CardName")
public SaveDeck getOneDeck(String NameOfDeck);
}
进一步创建了数据库类:
@Database(entities = {SaveDeck.class}, version = 1)
public abstract class SaveDecksDataBase extends RoomDatabase {
public abstract DeckBuilderDao deckBuilderDao();
}
最后一个类是片段,我尝试在其中填充数据库,而在populateDB()
类中是问题
公共类review_fragment扩展了片段{
private List<TransferDeck> mTransferDeck = DataHolder.getInstance().savedDecklistTransfer;
SaveDecksDataBase mSavedDecksDB;
Cursor mCursor;
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//return super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.review_fragment, container, false);
/*Introduce Cards Recycler*/
RecyclerView rvCards = view.findViewById(R.id.rv_review_cardlist);
rvCards.setLayoutManager(new GridLayoutManager(getActivity(), 5));
review_RViewAdapter_Cards adapterCards = new review_RViewAdapter_Cards(getContext(), mTransferDeck);
rvCards.setAdapter(adapterCards);
/*Init Room database*/
mSavedDecksDB = Room.databaseBuilder(getActivity(),SaveDecksDataBase.class,"SavedDecksDB.db").build();
populateDB(mTransferDeck);
return view;
}
private void populateDB(final List<TransferDeck> mTransferDeck) {
new Thread(new Runnable() {
@Override
public void run() {
List<SaveDeck> mSaveDeck = new ArrayList<>();
for(int i = 0; i<mTransferDeck.size(); i++){
mSaveDeck.add(new SaveDeck(i, "FirstSavedDeck", mTransferDeck.get(i).getCardImage() ,mTransferDeck.get(i).getTypeImage(), mTransferDeck.get(i).getCost(), mTransferDeck.get(i).getName(), mTransferDeck.get(i).getNumber()));
}
mSavedDecksDB.deckBuilderDao().insertCards(mSaveDeck);
}
}).start();
}
}
答案 0 :(得分:1)
我想提到这应该是评论而不是答案。
首先,使用AysncTask
或更强大的Executors.newSingleThreadExecutor()
。如果您喜欢第二个,那么最好创建一个辅助类(example)。示例:
private void populateDB(final List<TransferDeck> mTransferDeck) {
AppExecutors.diskIO().execute(() -> {
for(int i = 0; i<mTransferDeck.size(); i++){
mSavedDecksDB.deckBuilderDao().insertCards(new SaveDeck(...);
}
});
}
(1)创建一个空白的构造函数。
(4)不应在此处初始化Room Database,最好是Singleton。因此您的数据库类(3)可能像这样:
public abstract class SaveDecksDataBase extends RoomDatabase {
private static SaveDecksDataBase sINSTANCE;
private static final Object LOCK = new Object();
public static SaveDecksDataBase getDatabase(final Context context) {
if (sINSTANCE == null) {
synchronized (LOCK) {
if (sINSTANCE == null) {
sINSTANCE = Room.databaseBuilder(context.getApplicationContext(),
SaveDecksDataBase.class, "SavedDecksDB.db")
.build();
}
}
}
return sINSTANCE;
}
public abstract DeckBuilderDao deckBuilderDao();
}
最后,要获取SaveDeck
对象,您还必须使用Executors
或AsyncTask
在后台进行工作,然后填充RecyclerView
。