提问人:Ayushman Gupta 提问时间:11/6/2023 最后编辑:Alex MamoAyushman Gupta 更新时间:11/6/2023 访问量:53
无法在我的 java android 应用程序中从 Firebase 检索数据
Unable to Retrieve data from Firebase in my java android app
问:
Home.java 类
package com.example.rsvpea;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.rsvpea.Models.UploadEventModel;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class Home extends AppCompatActivity {
public static ImageButton home;
public static ImageButton bookings;
public static ImageButton profile;
FirebaseDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
home = findViewById(R.id.home);
bookings = findViewById(R.id.myBookings);
profile = findViewById(R.id.myProfile);
BottomNavBar buttonClickListenerHome = new BottomNavBar(home);
BottomNavBar buttonClickListenerBookings = new BottomNavBar(bookings);
BottomNavBar buttonClickListenerProfile = new BottomNavBar(profile);
ArrayList<TimelineCardContent> timelineCardContents = new ArrayList<>();
RecyclerView timeline = findViewById(R.id.timelineRecyclerView);
timeline.setLayoutManager(new LinearLayoutManager(this));
database=FirebaseDatabase.getInstance();
database.getReference().child("Event List").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for(DataSnapshot itemSnapshot : snapshot.getChildren()){
//String img = itemSnapshot.child("image").getValue(String.class);
//String eventTitle = itemSnapshot.child("eventTitle").getValue(String.class);
UploadEventModel temp = itemSnapshot.getValue(UploadEventModel.class);
timelineCardContents.add(new TimelineCardContent(temp.getImage(), temp.getEventTitle()));
}
Log.d("FirebaseData", "Data retrieved: " + timelineCardContents.size() + " items");
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(Home.this, "Error", Toast.LENGTH_SHORT).show();
}
});
RecycleContentAdapter adapter = new RecycleContentAdapter(this, timelineCardContents);
System.out.println(timelineCardContents);
timeline.setAdapter(adapter);
home.setOnClickListener(buttonClickListenerHome);
profile.setOnClickListener(buttonClickListenerProfile);
bookings.setOnClickListener(buttonClickListenerBookings);
findViewById(R.id.uploadnewevent).setOnClickListener(view -> {
Intent intent = new Intent(Home.this, UploadActivity.class);
Home.this.startActivity(intent);
});
}
}
适配器类
package com.example.rsvpea;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class RecycleContentAdapter extends RecyclerView.Adapter<RecycleContentAdapter.ViewHolder> {
Context context;
ArrayList<TimelineCardContent> arrContent;
RecycleContentAdapter(Context context, ArrayList<TimelineCardContent> arrContent){
this.context=context;
this.arrContent=arrContent;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v= LayoutInflater.from(context).inflate(R.layout.timeline_item, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
TimelineCardContent content = arrContent.get(position);
//holder.img.setImageResource(arrContent.get(position).img);
Picasso.get()
.load(content.img)
.into(holder.img);
holder.title.setText(arrContent.get(position).title);
holder.moreDetailsButton.setOnClickListener(v -> {
Intent intent = new Intent(v.getContext(), MoreDetails.class);
v.getContext().startActivity(intent);
});
}
@Override
public int getItemCount() {
return arrContent.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView title;
ImageView img;
Button moreDetailsButton;
public ViewHolder(@NonNull View itemView) {
super(itemView);
this.title=itemView.findViewById(R.id.title);
this.img=itemView.findViewById(R.id.img);
moreDetailsButton = itemView.findViewById(R.id.moreDetails);
}
}
}
这是我的事件模型类
package com.example.rsvpea.Models;
public class UploadEventModel {
private String eventTitle, Desc, date, venue, image;
public UploadEventModel(String eventTitle, String desc, String date, String venue, String image) {
this.eventTitle = eventTitle;
this.Desc = desc;
this.date = date;
this.venue = venue;
this.image = image;
}
public UploadEventModel(){}
public String getEventTitle() {
return eventTitle;
}
public void setEventTitle(String eventTitle) {
this.eventTitle = eventTitle;
}
public String getDesc() {
return Desc;
}
public void setDesc(String desc) {
Desc = desc;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getVenue() {
return venue;
}
public void setVenue(String venue) {
this.venue = venue;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
在我的 logcat 中,它显示 ArrayList 为空
2023-11-06 14:27:36.006 23637-23637 FirebaseData com.example.rsvpea D 检索的数据:0
下面是 JSON 文件:
{
"Events List": {
"5 Nov 2023 20:43:57": {
"date": "29 feb 2024",
"desc": "birthday party for a batak",
"eventTitle": "Batak Bday",
"image": "url",
"venue": "TMA Pai"
},
"5 Nov 2023 23:12:29": {
"date": "40 feb 2024 ",
"desc": "event description sample",
"eventTitle": "new event",
"image": "url",
"venue": "tma pai"
},
"6 Nov 2023 00:28:28": {
"date": "hhhs",
"desc": "hshsh",
"eventTitle": "hsha",
"image": "url"
}
},
"Users": {
"cN0Z4YQEeoXPQKYH0lYxQegpfFE2": {
"mail": "[email protected]",
"password": "hello123",
"userName": "ayu"
}
}
}
应用程序运行时不会出现任何错误,但显示空的回收器视图。 有什么帮助吗?
我没有使用模型,而是尝试像这样手动操作,但也没有用
String img = itemSnapshot.child("image").getValue(String.class);
String eventTitle = itemSnapshot.child("eventTitle").getValue(String.class);
这是我的数据库规则
{
"rules": {
".read": true, // 2023-12-4
".write": true, // 2023-12-4
}
}
答:
0赞
Alex Mamo
11/6/2023
#1
仔细查看您的代码和数据库架构,我发现问题来自以下代码行:
database.getReference().child("Event List").addValueEventListener(/* ... /*);
为什么?因为数据库中的节点称为“事件列表”而不是“事件列表”。看到有一个缺失。因此,要解决这个问题,请使用正确的参考:s
database.getReference().child("Events List").addValueEventListener(/* ... /*);
// 👆
评论
1赞
Ayushman Gupta
11/6/2023
谢谢一群人,我花了大量时间试图解决这个问题。你是救命稻草(对不起,我不能投票,因为我的帐户是新帐户)
评论
Data retrieved: 0
snapshot.getChildrenCount()