gpt4 book ai didi

java - 如何每次从 AddSingleValueEventListner 更新 onDataChange 时从 firebase 子节点更新子节点总数?

转载 作者:太空狗 更新时间:2023-10-29 13:44:26 27 4
gpt4 key购买 nike

下图显示了我的 Firebase 数据库结构:

enter image description here

所有数据检索成功。这是我的模型类。

public class Post
{
public String lastname;
public String postid;
public long timestamp;
public HashMap<String,Boolean> count;

public Post()
{
}

public Post(String lastname, long timestamp, String postid,HashMap count)
{
this.lastname=lastname;
this.timestamp=timestamp;
this.postid=postid;
this.count=count;
}

public HashMap<String, Boolean> getCounts() {
return count;
}

public void setCounts(HashMap<String, Boolean> count) {
this.count = count;
}

在主 Activity 中我用来获取数据

 mAdapter = new PostAdapter(MainActivity.this);

getAllPost(null);
postList.addOnScrollListener(new RecyclerView.OnScrollListener() {

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

if (!recyclerView.canScrollVertically(1))
{

loaded=loaded+10;
if (totalPost== mAdapter.getItemCount())
{
Toast.makeText(MainActivity.this, "no more post", Toast.LENGTH_SHORT).show();
}
else
{
getAllPost(mAdapter.getLastItemId());
}

}
}
});
postList.setAdapter(mAdapter);
private void getAllPost(final String nodeId)
{
final Query query;
final int left= (int) (totalPost-mAdapter.getItemCount());
Toast.makeText(this, String .valueOf(left), Toast.LENGTH_SHORT).show();


if (nodeId == null)
{
query = PostRef
.orderByChild("timestamp")
.limitToLast(mPostsPerPage);
}

else
{

if (left<10)
{
query = PostRef
.orderByChild("timestamp")
.limitToFirst(left);
}
else
{
Long time=Long.parseLong(nodeId);
query = PostRef
.orderByChild("timestamp").endAt(time)
.limitToLast(10);
}

}


query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<Post> userModels = new ArrayList<>();


for (DataSnapshot userSnapshot : dataSnapshot.getChildren())
{
userModels.add(userSnapshot.getValue(Post.class));

}
if (!(nodeId ==null))
{
if (left>10)
{
userModels.remove(9);
}

}


Collections.reverse(userModels);
mAdapter.addAll(userModels);
}

@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
}

在适配器中:

      public class PostAdapter extends RecyclerView.Adapter<PostHolder>
{
List<Post> mPost;
Context mContext;
public PostAdapter(Context c) {
this.mPost = new ArrayList<>();
mContext=c;
}
@NonNull
@Override
public PostHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new PostHolder(LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.all_post_layout, viewGroup, false));
}
@Override
public void onBindViewHolder(@NonNull final PostHolder postHolder, final int i) {
final String PostKey=mPost.get(i).getPostid();
FirebaseAuth mAuth=FirebaseAuth.getInstance();
final String currentUserID=mAuth.getCurrentUser().getUid();


final DatabaseReference post=FirebaseDatabase.getInstance().getReference().child("Posts");
showCounts(postHolder,i);
setCountsButton(postHolder,i,currentUserID);
tapOnCounts(postHolder,i,currentUserID,post,PostKey);
}
private void tapOncounts(final PostHolder postHolder, final int i, final String currentUserID, final DatabaseReference post, final String postKey)
{
postHolder.countsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
if (mPost.get(i).getCounts() !=null)
{
if(mPost.get(i).getCounts().containsKey(currentUserID))
{
post.child(postKey).child("counts").child(currentUserID).removeValue();
postHolder.countsButton.setImageResource(R.drawable.discounts);
}
else
{
postHolder.countsButton.setImageResource(R.drawable.counts);
post.child(postKey).child("counts").child(currentUserID).setValue(true);
}
}
else
{
postHolder.countsButton.setImageResource(R.drawable.counts);
post.child(postKey).child("counts").child(currentUserID).setValue(true);
}
}
});
}
private void setcountsButton(final PostHolder postHolder, int i, String currentUserID)
{
if (mPost.get(i).getCounts() !=null)
{
if(mPost.get(i).getCounts().containsKey(currentUserID))
{
postHolder.countsButton.setImageResource(R.drawable.counts);
}
else
{
postHolder.countsButton.setImageResource(R.drawable.discounts);
}
}
}
private void showCounts(PostHolder postHolder, int i)
{
if((mPost.get(i).getCounts() !=null))
{
postHolder.noOfcounts.setText(String.valueOf(mPost.get(i).getCounts().size()));
}
else
{
postHolder.noOfcounts.setText("0");
}
}
@Override
public int getItemCount() {
return mPost.size();
}
public void addAll(List<Post> newPost) {
int initialSize = mPost.size();
mPost.addAll(newPost);
notifyItemRangeInserted(initialSize, newPost.size());
}

public String getLastItemId() {
return String.valueOf(mPost.get(mPost.size() - 1).getTimestamp());
}

}

一切都成功,但总没有。计数节点 recylerview 中的子更改(新子添加或旧子删除)未更新。它只会在我尝试进行另一项 Activity 并在 MainActivity 中重新运行时更新。

最佳答案

要获得实时更新,您应该使用 Query 的 addValueEventListener(ValueEventListener listener)方法:

Add a listener for changes in the data at this location.

使用 addListenerForSingleValueEvent(ValueEventListener listener) 时:

Add a listener for a single change in the data at this location.

编辑:

要获取列表的大小,请更改以下代码行:

holder.count.setText(String.valueOf(mPost.get(i).getCount().size));

holder.count.setText(String.valueOf(getItemCount());

关于java - 如何每次从 AddSingleValueEventListner 更新 onDataChange 时从 firebase 子节点更新子节点总数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56948433/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com