Android firebase-为什么单击星号总是删除一个子值?
这是FreeboardPost.javaAndroid firebase-为什么单击星号总是删除一个子值?,android,firebase,Android,Firebase,这是FreeboardPost.java public class FreeboardPost { public String imageUrl; public String uid; public String author; public String title; public String body; public int starCount = 0; public Map<String, Boolean> star
public class FreeboardPost
{
public String imageUrl;
public String uid;
public String author;
public String title;
public String body;
public int starCount = 0;
public Map<String, Boolean> stars = new HashMap<>();
public FreeboardPost()
{
}
public FreeboardPost(String uid, String author, String title, String body, String imageUrl)
{
this.imageUrl = imageUrl;
this.uid = uid;
this.author = author;
this.title = title;
this.body = body;
}
@Exclude
public Map<String, Object> toMap()
{
HashMap<String, Object> result = new HashMap<>();
result.put("image", imageUrl);
result.put("uid", uid);
result.put("author", author);
result.put("title", title);
result.put("body", body);
result.put("starCount", starCount);
result.put("stars", stars);
return result;
}
}
这是FreeboardPostListFragment.java
public abstract class FreeboardPostListFragment extends Fragment
{
private static final String TAG = "FreeBoardPostListFragment";
private DatabaseReference mDatabase;
private FirebaseRecyclerAdapter<FreeboardPost, FreeboardPostViewHolder> mAdapter;
private RecyclerView mRecycler;
private LinearLayoutManager mManager;
private DatabaseReference mPostReference;
private ValueEventListener mPostListener;
private String image;
public FreeboardPostListFragment()
{
}
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.freeboard_fragment_all_posts, container, false);
mDatabase = FirebaseDatabase.getInstance().getReference();
mRecycler = (RecyclerView)rootView.findViewById(R.id.freeboard_messages_list);
mRecycler.setHasFixedSize(true);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
mManager = new LinearLayoutManager(getActivity());
mManager.setReverseLayout(true);
mManager.setStackFromEnd(true);
mRecycler.setLayoutManager(mManager);
Query postsQuery = getQuery(mDatabase);
mAdapter = new FirebaseRecyclerAdapter<FreeboardPost, FreeboardPostViewHolder>(FreeboardPost.class, R.layout.freeboard_item_post, FreeboardPostViewHolder.class, postsQuery)
{
@Override
protected void populateViewHolder(final FreeboardPostViewHolder viewHolder, final FreeboardPost model, final int position)
{
final DatabaseReference postRef = getRef(position);
final String postKey = postRef.getKey();
mPostReference = FirebaseDatabase.getInstance().getReference().child("FreeboardPost").child(postKey);
viewHolder.itemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(getActivity(), FreeBoardPostDetailActivity.class);
intent.putExtra(FreeBoardPostDetailActivity.EXTRA_POST_KEY, postKey);
startActivity(intent);
}
});
if(model.stars.containsKey(getUid()))
{
viewHolder.freeboardstarView.setImageResource(R.drawable.ic_toggle_star_24);
}
else
{
viewHolder.freeboardstarView.setImageResource(R.drawable.ic_toggle_star_outline_24);
}
viewHolder.bindToPost(model, new View.OnClickListener()
{
@Override
public void onClick(View starView)
{
DatabaseReference globalPostRef = mDatabase.child("FreeboardPost").child(postRef.getKey());
DatabaseReference userPostRef = mDatabase.child("FreeboardUserPost").child(model.uid).child(postRef.getKey());
onStarClicked(globalPostRef);
onStarClicked(userPostRef);
}
});
}
};
mRecycler.setAdapter(mAdapter);
}
private void onStarClicked(DatabaseReference postRef)
{
postRef.runTransaction(new Transaction.Handler()
{
@Override
public Transaction.Result doTransaction(MutableData mutableData)
{
FreeboardPost p = mutableData.getValue(FreeboardPost.class);
if(p == null)
{
return Transaction.success(mutableData);
}
if(p.stars.containsKey(getUid()))
{
p.starCount = p.starCount - 1;
p.stars.remove(getUid());
}
else
{
p.starCount = p.starCount + 1;
p.stars.put(getUid(), true);
}
mutableData.setValue(p);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot)
{
}
});
}
public String getUid()
{
return FirebaseAuth.getInstance().getCurrentUser().getUid();
}
public abstract Query getQuery(DatabaseReference databaseReference);
}
公共抽象类FreeboardPostListFragment扩展了片段
{
私有静态最终字符串标记=“FreeBoardPostListFragment”;
私有数据库参考数据库;
私人FirebaseRecyclerAdapter mAdapter;
私人垃圾回收站;
私人直线酒店经理/经理;
私有数据库引用MPostreeference;
私有值事件监听器mPostListener;
私有字符串图像;
公共干舷PostListFragment()
{
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态)
{
super.onCreateView(充气机、容器、保存状态);
视图根视图=充气机。充气(R.layout.freeboard\u fragment\u all\u Post,container,false);
mDatabase=FirebaseDatabase.getInstance().getReference();
mRecycler=(RecyclerView)rootView.findviewbyd(R.id.freeboard\u messages\u list);
mRecycler.setHasFixedSize(true);
返回rootView;
}
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
mManager=newlinearlayoutmanager(getActivity());
mManager.setReverseLayout(true);
mManager.setStackFromEnd(true);
mRecycler.setLayoutManager(mManager);
Query postsQuery=getQuery(mDatabase);
mAdapter=新FirebaseRecyclerAdapter(FreeboardPost.class,R.layout.freeboard\u item\u post,FreeboardPostViewHolder.class,postsQuery)
{
@凌驾
受保护的空心填充视图支架(最终干舷后视图支架视图支架,最终干舷后模型,最终内部位置)
{
最终数据库参考postRef=getRef(位置);
最后一个字符串postKey=postRef.getKey();
mPostReference=FirebaseDatabase.getInstance().getReference().child(“FreeboardPost”).child(postKey);
viewHolder.itemView.setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
Intent Intent=新的Intent(getActivity(),FreeBoardPostDetailActivity.class);
intent.putExtra(FreeBoardPostDetailActivity.EXTRA\u POST\u键,postKey);
星触觉(意向);
}
});
if(model.stars.containsKey(getUid()))
{
viewHolder.FreeboardInghlightView.setImageResource(R.drawable.ic_toggle_star_24);
}
其他的
{
viewHolder.FreeboardInghlightView.setImageResource(R.drawable.ic_toggle_star_outline_24);
}
viewHolder.bindToPost(model,newview.OnClickListener()
{
@凌驾
公共void onClick(视图视图)
{
DatabaseReference globalPostRef=mDatabase.child(“FreeboardPost”).child(postRef.getKey());
DatabaseReference userPostRef=mDatabase.child(“FreeboardUserPost”).child(model.uid).child(postRef.getKey());
onStarClicked(globalPostRef);
onStarClicked(userPostRef);
}
});
}
};
mRecycler.setAdapter(mAdapter);
}
私有void onStarClicked(数据库引用postRef)
{
postRef.runTransaction(新事务处理程序()
{
@凌驾
公共事务.Result doTransaction(MutableData MutableData)
{
FreeboardPost p=mutableData.getValue(FreeboardPost.class);
if(p==null)
{
返回事务。成功(可变数据);
}
if(p.stars.containsKey(getUid()))
{
p、 starCount=p.starCount-1;
p、 删除(getUid());
}
其他的
{
p、 starCount=p.starCount+1;
p、 stars.put(getUid(),true);
}
可变数据设置值(p);
返回事务。成功(可变数据);
}
@凌驾
public void onComplete(DatabaseError DatabaseError、布尔b、DataSnapshot DataSnapshot)
{
}
});
}
公共字符串getUid()
{
返回FirebaseAuth.getInstance().getCurrentUser().getUid();
}
公共摘要查询getQuery(DatabaseReference DatabaseReference);
}
这是freeboard_include_post_author.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center_vertical"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/freeboard_post_author_photo"
android:layout_width="40dp"
android:layout_height="40dp"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000">
</de.hdodenhof.circleimageview.CircleImageView>
<TextView
android:id="@+id/freeboard_post_author"
style="@style/Base.TextAppearance.AppCompat.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
tools:text="someauthor@email.com" />
</LinearLayout>
这是freeboard_item_post.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<ImageView
android:id="@+id/freeboard_post_image"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginRight="10dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:scaleType="centerCrop"
android:src="@drawable/profile"/>
<RelativeLayout
android:id="@+id/freeboard_text_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/freeboard_post_image">
<LinearLayout
android:id="@+id/freeboard_star_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/freeboard_post_author_layout"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/freeboard_post_author_layout"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/freeboard_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:background="?attr/selectableItemBackground"
android:src="@drawable/ic_toggle_star_outline_24" />
<TextView
android:id="@+id/freeboard_post_num_stars"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
tools:text="7" />
</LinearLayout>
<include
android:id="@+id/freeboard_post_text"
layout="@layout/freeboard_include_post_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp" />
<include
android:id="@+id/freeboard_post_author_layout"
android:layout_below="@+id/freeboard_post_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="@layout/freeboard_include_post_author"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
这是在我点击星号按钮之后。
我100%确定没有删除子项或值的代码。。但是为什么它会从孩子身上删除我的图像地址。。?我对它很好奇。谢谢大家! 您没有在数据库中发布创建FreeboardPost
实例的代码。我猜您使用的是toMap()
,因为它将图像URL存储在名为image
的字段中,该字段出现在您发布的数据库示例中
在您的事务中,您使用Firebase对FreeboardPost进行序列化/反序列化。它希望在名为imageUrl
的字段中找到图像URL,因为这是类中的公共字段名。读取您发布的节点时,没有名为imageUrl
的字段,因此创建的FreeboardPost
实例的该字段值为空。写入实例时,没有为imageUrl
写入值,因为该值为null,并且
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<ImageView
android:id="@+id/freeboard_post_image"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginRight="10dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:scaleType="centerCrop"
android:src="@drawable/profile"/>
<RelativeLayout
android:id="@+id/freeboard_text_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/freeboard_post_image">
<LinearLayout
android:id="@+id/freeboard_star_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/freeboard_post_author_layout"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/freeboard_post_author_layout"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/freeboard_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:background="?attr/selectableItemBackground"
android:src="@drawable/ic_toggle_star_outline_24" />
<TextView
android:id="@+id/freeboard_post_num_stars"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
tools:text="7" />
</LinearLayout>
<include
android:id="@+id/freeboard_post_text"
layout="@layout/freeboard_include_post_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp" />
<include
android:id="@+id/freeboard_post_author_layout"
android:layout_below="@+id/freeboard_post_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="@layout/freeboard_include_post_author"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>