[英]How to retrieve all value from firebase?
DATABASE数据库
I wanted to retrieve all the value from my firebase, but it retrieved the last one only which is "hjh" , what did i do wrong?我想从我的 firebase 中检索所有值,但它只检索了最后一个“hjh”,我做错了什么?
This is my code这是我的代码
public class MainActivity extends AppCompatActivity {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("content");
ArrayList<String> postList = new ArrayList<>();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference contentRef = rootRef.child("Post");
ValueEventListener value_l;
TextView content_view;
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
//setContentView(R.layout.post_info);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
content_view = (TextView) findViewById(R.id.contentTextview);
listview = (ListView) findViewById(R.id.listview);
value_l = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()){
String contentText = ds.child("content").getValue(String.class);
postList.add(contentText);
//FOR ARRAYLIST TESTING content_view.setText(postList.get(postList.size()-1));
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { }
};
Thank you :)谢谢 :)
You should try this code it may be help you.你应该试试这个代码它可能对你有帮助。
1) MainActivity 1) 主活动
public class MainActivity extends AppCompatActivity {
private RecyclerView rvData;
private RetriveDataAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rvData = findViewById(R.id.rvData);
rvData.setHasFixedSize(true);
rvData.setLayoutManager(new LinearLayoutManager(MainActivity.this));
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
database.child("Post").addValueEventListener(new ValueEventListener() {
@SuppressLint("LongLogTag")
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<MyBean> arrayList = new ArrayList<MyBean>();
for (DataSnapshot noteDataSnapshot : dataSnapshot.getChildren()) {
MyBean bean = noteDataSnapshot.getValue(MyBean.class);
arrayList.add(bean);
}
Log.e("MainActivity.this", "Data list ::>>>>>" + arrayList.size());
adapter = new RetriveDataAdapter(MainActivity.this, arrayList);
rvData.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
2) MyBean 2) 豆豆
public class MyBean {
public String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
3) RetriveDataAdapter 3) RetriveDataAdapter
public class RetriveDataAdapter extends RecyclerView.Adapter<RetriveDataAdapter.MyViewHolder> {
private static final String TAG = "RetriveDataAdapter.class";
private Context ctx;
private ArrayList<MyBean> dataList;
@SuppressLint("LongLogTag")
public RetriveDataAdapter(Context context, ArrayList<MyBean> dataList) {
this.ctx = context;
this.dataList = dataList;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView tvContent;
public MyViewHolder(View view) {
super(view);
tvContent = view.findViewById(R.id.tvContent);
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_form, parent, false);
return new MyViewHolder(itemView);
}
@SuppressLint("LongLogTag")
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final MyBean bean = dataList.get(position);
holder.tvContent.setText("Content: " + bean.getContent());
}
@Override
public int getItemCount() {
return dataList.size();
}
}
I can't see your whole Database's structure and didn't see where you call addValueEventListener(value_l)
, so that I can only guess it's because of the wrong level Reference
.我看不到你整个数据库的结构,也没有看到你在哪里调用addValueEventListener(value_l)
,所以我只能猜测这是因为错误的级别Reference
。
You should add value_l
to the correct reference, maybe all the post's parent, and iterate through all of it's child using snapshot.forEach()
您应该将value_l
添加到正确的引用中,可能是所有帖子的父级,并使用snapshot.forEach()
遍历所有子级
Google for firebase database iterate
may provides more detailed information. Google for firebase database iterate
可能会提供更详细的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.