我正在使用android应用,正在Firebase中写入一些数据
{
"movies" : {
"movie2" : {
"movieId" : "movie2",
"movieReleaseDate" : "7/10/2018",
"movieThumbnail" : "http://flp.businessofboxoffice.com/images/kabali.jpg",
"movieTitle" : "Kabali (2018 Film) - Official Trailer",
"movieYoutubeLink" : "https://youtu.be/9wY_vb4pkLs"
},
"movie3" : {
"movieId" : "movie3",
"movieReleaseDate" : "7/10/2018",
"movieThumbnail" : "http://flp.businessofboxoffice.com/images/pic09.jpg",
"movieTitle" : "Movie Demo (2018 Film) - Official Trailer",
"movieYoutubeLink" : "https://youtu.be/9wY_vb4pkLs"
}
}
}
此后,我要在movieLotteryTicket
节点下添加一个子节点movie2
(单击相同),将更新后的movie2
节点添加到名为userFavourites
的不同部分下。
但是我没有在movie2
userFavourites
节点
{
"movies" : {
"movie2" : {
"movieId" : "movie2",
"movieReleaseDate" : "7/10/2018",
"movieLotteryTicket" : {
"aaaa@gmail.com" : "12300"
},
"movieThumbnail" : "http://flp.businessofboxoffice.com/images/kabali.jpg",
"movieTitle" : "Kabali (2018 Film) - Official Trailer",
"movieYoutubeLink" : "https://youtu.be/9wY_vb4pkLs"
},
"movie3" : {
"movieId" : "movie3",
"movieReleaseDate" : "7/10/2018",
"movieThumbnail" : "http://flp.businessofboxoffice.com/images/pic09.jpg",
"movieTitle" : "Movie Demo (2018 Film) - Official Trailer",
"movieYoutubeLink" : "https://youtu.be/9wY_vb4pkLs"
}
},
"userFavourites" : {
"bbbb@gmail,com" : {
"movie2" : {
"movieId" : "movie2",
"movieReleaseDate" : "7/10/2018",
"movieThumbnail" : "http://flp.businessofboxoffice.com/images/kabali.jpg",
"movieTitle" : "Kabali (2018 Film) - Official Trailer",
"movieYoutubeLink" : "https://youtu.be/9wY_vb4pkLs"
}
}
}
}
当我删除movie2
中的userFavourites
节点并再次使用相同的按钮添加时,现在我得到正确更新的movie2
节点
{
"movies" : {
"movie2" : {
"movieId" : "movie2",
"movieReleaseDate" : "7/10/2018",
"movieLotteryTicket" : {
"aaaa@gmail.com" : "12300"
},
"movieThumbnail" : "http://flp.businessofboxoffice.com/images/kabali.jpg",
"movieTitle" : "Kabali (2018 Film) - Official Trailer",
"movieYoutubeLink" : "https://youtu.be/9wY_vb4pkLs"
},
"movie3" : {
"movieId" : "movie3",
"movieReleaseDate" : "7/10/2018",
"movieThumbnail" : "http://flp.businessofboxoffice.com/images/pic09.jpg",
"movieTitle" : "Movie Demo (2018 Film) - Official Trailer",
"movieYoutubeLink" : "https://youtu.be/9wY_vb4pkLs"
}
},
"userFavourites" : {
"bbbb@gmail,com" : {
"movie2" : {
"movieId" : "movie2",
"movieReleaseDate" : "7/10/2018",
"movieLotteryTicket" : {
"aaaa@gmail.com" : "12300"
},
"movieThumbnail" : "http://flp.businessofboxoffice.com/images/kabali.jpg",
"movieTitle" : "Kabali (2018 Film) - Official Trailer",
"movieYoutubeLink" : "https://youtu.be/9wY_vb4pkLs"
}
}
}
}
这是我单击按钮时的代码
public void OnMovieClicked(final Movie movie, View view) {
// geting movieLotteryTicket
RelativeLayout relativeLayout = ((RelativeLayout) view);
StringBuilder lottery = new StringBuilder();
for (int i = 0; i < relativeLayout.getChildCount();i++){
View v = relativeLayout.getChildAt(i);
if(v.getId()==R.id.checkBoxLottery1){
if(((CheckBox)v).isChecked()){
lottery.append("1");
}
else {
lottery.append("0");
}
}
if(v.getId()==R.id.checkBoxLottery2){
if(((CheckBox)v).isChecked()){
lottery.append("2");
}
else {
lottery.append("0");
}
}
if(v.getId()==R.id.checkBoxLottery3){
if(((CheckBox)v).isChecked()){
lottery.append("3");
}
else {
lottery.append("0");
}
}
if(v.getId()==R.id.checkBoxLottery4){
if(((CheckBox)v).isChecked()){
lottery.append("4");
}
else {
lottery.append("0");
}
}
if(v.getId()==R.id.checkBoxLottery5){
if(((CheckBox)v).isChecked()){
lottery.append("5");
}
else {
lottery.append("0");
}
}
}
final String finalLottery = lottery.toString();
final DatabaseReference lotteryDatabaseReference;
lotteryDatabaseReference = FirebaseDatabase.getInstance().getReference().child("movies").child(movie.getmovieId()).child("movieLotteryTicket");
lotteryDatabaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.child(Constants.encodeEmail(mUserEmailString)).getValue()!=null){
Toast.makeText(getActivity(),"Already bought Lottery for this movie ",Toast.LENGTH_SHORT).show();
}
else {
dataSnapshot.getRef().child(Constants.encodeEmail(mUserEmailString)).setValue(finalLottery);
Toasa t.makeText(getActivity(),"Thanks for buying lottery tickets....",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
// adding movie to user Favourites
if (Constants.isIncludedInMap(mFavouriteMoviesMarkedMap,movie)){
mGetAllFavouriteMoviesMarkedReference.child(movie.getmovieId())
.removeValue();
mCompositeSubscription.add(mLiveMoviesService.addOrRemoveFavourite(mSocket,mUserEmailString,
movie.getmovieId(),"1"));
} else{
mGetAllFavouriteMoviesMarkedReference.child(movie.getmovieId())
.setValue(movie);
mCompositeSubscription.add(mLiveMoviesService.addOrRemoveFavourite(mSocket,mUserEmailString,
movie.getmovieId(),"0"));
}
}