Beginner Coder
Beginner Coder

Reputation: 53

How to retrieve all value from firebase?

DATABASE

enter image description here

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?

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 :)

Upvotes: 1

Views: 97

Answers (2)

Kinjal
Kinjal

Reputation: 142

You should try this code it may be help you.

1) MainActivity

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

public class MyBean {

    public String content;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

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();
    }
}

Upvotes: 1

Wesely
Wesely

Reputation: 1475

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.

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()

Google for firebase database iterate may provides more detailed information.

Upvotes: 0

Related Questions