我有一个Card对象,其中有一个“兴趣”对象列表。 我正在尝试将卡存储在Firestore上,以便以后再取回。 但是,每次我成功将“ CArd”存储在数据库中时,我都无法对设备上的每个卡对象进行更改。具体来说,我无法修改其中列表中的每个兴趣。 即。我无法向HashMap中的每张卡添加新兴趣:cardNameToCard
有人可以帮助我了解我在做什么错吗?我怀疑CardMap中每张卡的内存指针都在变化。我尝试为每个新列表和每个新卡添加新适配器。我还尝试制作一张重复的卡,然后将该数据传递给函数,然后等待回调。 存储数据的MainActivity函数:
` 公共静态MapcardNameToCard = new HashMap <>(); fs.storeCards(cardName,duplicateCardMapping,new MyCallBack(){
@Override
public void onCardStoredCallback(Map<String, Card> returnedCardMap) {
//cardNameToCard.putAll(returnedCardMap);
for(String cardName:returnedCardMap.keySet()){
cardNameToCard.get(cardName).setStoredInterests(returnedCardMap.get(cardName).storedInterests);
}
这是一张卡片的样子:
String userEmailId;
String cardName;
List<Interest> storedInterests;
boolean visible;
这是我在Firestore中的存储功能:它检查Card是否存在于数据库中。如果有,它将更新,否则将创建一个新文档
//need to confirm if working properly or not!
public void storeCards(final String cardName, final Map<String,Card>cardNameToCard, final MyCallBack myCallBack) {
Log.d(TAG,"STORING CARDS");
FirebaseFirestore db=FirebaseFirestore.getInstance();
db.collection("cards")
.whereEqualTo("cardName",cardName)
.whereEqualTo("userEmailId",Main3Activity.getThisUser().getEmail())
.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
Set<String> cardNamesUpdated=new HashSet<>();
for(QueryDocumentSnapshot documentSnapshot:queryDocumentSnapshots){
String idOfThisCard=documentSnapshot.getId();
cardNamesUpdated.add((String) documentSnapshot.getData().get("cardName"));
Card x=cardNameToCard.get(cardName);
List<Interest> interestCurrent=x.getStoredInterests();
List<Interest> alternateList=new ArrayList<>();
for(Interest i:interestCurrent){
if(i.getName().isEmpty()||i.getName()==null){
alternateList.add(new Interest("",cardName));
}else {
alternateList.add(i);
}
Log.d(TAG,"I AM ADDING "+i.getName());
}
cardNameToCard.get(cardName).storedInterests=alternateList;
updateOneCard(idOfThisCard, Objects.requireNonNull(cardNameToCard.get(cardName)).convertToMap(),cardNameToCard,myCallBack);//updating this guy
}
//cardNameSet.removeAll(cardNamesUpdated);//the guys who are new and need to be added as brand new cards
if(queryDocumentSnapshots.size()==0)//if we did not get any result on our search for this card\
addNewCard(cardName,cardNameToCard,myCallBack);
}
});
}
addNewCard和updateOneCard:
//need to confirm if working properly or not!
private void addNewCard(final String x, final Map<String, Card> cardNameToCard, final MyCallBack myCallBack) {
//to ensure that the schema does not change in case of no interests added by user, we will create a blank interest
if(cardNameToCard.get(x).getStoredInterests().isEmpty()){
//Main3Activity.cardNameToCard
Interest blankInterest=new Interest("",x);
Card cardToReplace=cardNameToCard.get(x);
cardToReplace.storedInterests=(new ArrayList<Interest>(Arrays.asList(new Interest[]{blankInterest})));
cardNameToCard.replace(x,cardToReplace);
Log.d(TAG,"WE HAVE ADDED A BLANK INTEREST IN CARDNAME "+x);
}
FirebaseFirestore db=FirebaseFirestore.getInstance();
db.collection("cards")
//.add(cardNameToCard.get(x).convertToMap())
.add(cardNameToCard.get(x).convertToMap())
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Log.d(TAG,"WE HAVE ADDED A NEW CARD"+x);
myCallBack.onCardStoredCallback(cardNameToCard);
}
});
}
private void updateOneCard(String idOfThisCard, final Map<String, Object> data, final Map<String, Card> cardNameToCard,final MyCallBack myCallBack) {
FirebaseFirestore db=FirebaseFirestore.getInstance();
db.collection("cards")
.document(idOfThisCard)
.set(data)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG,"WE HAVE UPDATED AN EXISTING CARD "+data.get("cardName"));
myCallBack.onCardStoredCallback(cardNameToCard);
}
});
}```
Any help would be appreciated!!!