當(dāng)我想從ModelView類中的Firebase和SetValue恢復(fù)數(shù)據(jù)時(shí),會(huì)收到警告。我檢查數(shù)據(jù)庫和ModelView類中的所有數(shù)據(jù)。ModelView字符串名稱的鍵名是相同的,并且我在類中創(chuàng)建了setter方法。我不明白問題出在哪里。
我的ModelView類代碼:
public class PostGeter {
private String body;
private String date;
private String id;
private String image;
private String reaction;
private String status;
private String title;
private String userId;
public PostGeter() {}
public PostGeter(String body, String date, String id, String image, String reaction, String status, String title, String userId) {
this.body = body;
this.date = date;
this.id = id;
this.image = image;
this.reaction = reaction;
this.status = status;
this.title = title;
this.userId = userId;
}
public void setBody(String body) {
this.body = body;
}
public void setDate(String date) {
this.date = date;
}
public void setId(String id) {
this.id = id;
}
public void setImage(String image) {
this.image = image;
}
public void setReaction(String reaction) {
this.reaction = reaction;
}
public void setStatus(String status) {
this.status = status;
}
public void setTitle(String title) {
this.title = title;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getBody() {
return body;
}
public String getDate() {
return date;
}
public String getId() {
return id;
}
public String getImage() {
return image;
}
public String getReaction() {
return reaction;
}
public String getStatus() {
return status;
}
public String getTitle() {
return title;
}
public String getUserId() {
return userId;
}
我的適配器類:
public class postAdapter extends RecyclerView.Adapter<postAdapter.ViewHolder> {
Context context;
ArrayList<PostGeter> list;
public postAdapter(Context context, ArrayList<PostGeter> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public postAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.post_content_view, parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull postAdapter.ViewHolder holder, int position) {
PostGeter geter = list.get(position);
holder.title.setText(geter.getTitle());
holder.content.setText(geter.getBody());
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView img; TextView title,content;
public ViewHolder(@NonNull View itemView) {
super(itemView);
img = itemView.findViewById(R.id.postImageView);
title = itemView.findViewById(R.id.post_view_title);
content = itemView.findViewById(R.id.post_view_content);
}
}
}
我的firebase Data調(diào)用方法:
RecyclerView recyclerView = binding.postView;
recyclerView.setHasFixedSize(true);
LinearLayoutManager manager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(manager);
ArrayList list = new ArrayList<>();
postAdapter customAdapter = new postAdapter(getContext(), list);
recyclerView.setAdapter(customAdapter);
database = FirebaseDatabase.getInstance();
reference = database.getReference().child("posts");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot ds : snapshot.getChildren()){
for (DataSnapshot data : ds.getChildren()){
System.out.println(data);
PostGeter msg = ds.getValue(PostGeter.class);
list.add( msg);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
throw error.toException();
}
});
my log:
I/zygote64: After code cache collection, code=61KB, data=59KB
I/zygote64: Increasing code cache capacity to 256KB
D/FA: Application going to the background
I/System.out: DataSnapshot { key = 10843, value = {body=fasiaufsuie, title=ddddj} }
W/ClassMapper: No setter/field for 10843 found on class com.hello.writer.postAdapter.PostGeter
W/ClassMapper: No setter/field for 67903 found on class com.hello.writer.postAdapter.PostGeter
I/System.out: DataSnapshot { key = 67903, value = {date=06.03.2023, image=https://firebasestorage.googleapis.com/v0/b/hello-writer-2db52.appspot.com/o/posts%2FuserId%2F67903?alt=media&token=5bc42574-fe50-424a-8e74-af7aae7e33d0, reaction=0, id=67903, body=jgugjvvhv, title=vhhvvhuv, userId=user, status=approved} }
W/ClassMapper: No setter/field for 10843 found on class com.hello.writer.postAdapter.PostGeter
W/ClassMapper: No setter/field for 67903 found on class com.hello.writer.postAdapter.PostGeter
V/FA: Inactivity, disconnecting from the service
I/System.out: DataSnapshot { key = 67903, value = {date=06.03.2023, image=https://firebasestorage.googleapis.com/v0/b/hello-writer-2db52.appspot.com/o/posts%2FuserId%2F67903?alt=media&token=5bc42574-fe50-424a-8e74-af7aae7e33d0, reaction=0, id=67903, body=jgugjvvhv, title=vhhvvhuv, userId=user, status=approved} }
W/ClassMapper: No setter/field for 67903 found on class com.hello.writer.postAdapter.PostGeter
我想在recyclerView中顯示數(shù)據(jù)。我的數(shù)據(jù)庫JSON鏈接在這里:
我的數(shù)據(jù)庫json
您將收到以下警告:
因?yàn)槟噲D使用以下代碼行將鍵轉(zhuǎn)換為
PostGeter
類型的對(duì)象:要解決此問題,必須使用正確的
DataSnapshot
對(duì)象。因此,請(qǐng)將上述代碼行更改為: