0

so what should be changed in adapter which populates new cards when a child is added in firebase,im sending data through an object of modal class.Now its only makes 1 card on which newly added data is updated on.No new cards are being populated.and i dont want to get the whole data again and again,i just wanted to get data only one time and it should be retreived when a child is added on because the admin has to approve the patients data(and then it should be removed thereafter).

myadapter

public class Adapter extends RecyclerView.Adapter<Adapter.Viewholder> {


    public UserInformation value = new UserInformation();


    public Adapter(UserInformation value) {
        this.value = value;

    }


    @Override
    public Viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.item_user_layout, parent, false);
        return new Viewholder(view);

    }

    @Override
    public void onBindViewHolder(Viewholder holder, int position) {

        holder.edt_blood_group.setText(value.getBlood_group());
        holder.edt_contact_no.setText(value.getContact_no());
        holder.btn_approve.setText("approve");
    }

    @Override
    public int getItemCount() {
        return 3; //i dont know what to write here as i have no array of data
    }

    public class Viewholder extends RecyclerView.ViewHolder {
        EditText edt_blood_group;
        EditText edt_contact_no;
        Button btn_approve;

        public Viewholder(View itemView) {
            super(itemView);
            edt_blood_group = (EditText) itemView.findViewById(R.id.edt_blood_group);
            edt_contact_no = (EditText) itemView.findViewById(R.id.edt_contact_no);
             btn_approve = (Button) itemView.findViewById(R.id.btn_approve);

        }
    }
}

my MainActivity

     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final RecyclerView rcc = (RecyclerView) findViewById(R.id.userList);
        rcc.setLayoutManager(new LinearLayoutManager(this));
        //edtobj= (EditText) findViewById(R.id.edt_id);

        FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
        DatabaseReference reference = firebaseDatabase.getReference("Patients");
        reference.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                UserInformation newPost = dataSnapshot.getValue(UserInformation.class);
                rcc.setAdapter(new Adapter(newPost));

            }

item_layout

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <EditText
            android:id="@+id/edt_blood_group"
            android:layout_width="160dp"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/edt_contact_no"
            android:layout_width="160dp"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/btn_approve"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="approve" />


    </LinearLayout>
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • The adapter should take user information array or arraylist... not a single item! – Xenolion Jul 14 '18 at 08:14
  • and that array is supposed to have a complete snapshot of data or the last newly added data? and which listener is suitable for this purpose. – Umar Farooq Khan Jul 14 '18 at 08:28
  • The array list is better if you want to be updating your data, add the data and the recycler view will appear with n+1 items each time you add. – Xenolion Jul 14 '18 at 08:29
  • got it,but the data received from firebase is the "complete" snapshot of the data(whole data) everytime? or just a new child is added to the list,and that list is again populated? – Umar Farooq Khan Jul 14 '18 at 09:33
  • You are getting a single snapshot each time, not a whole data! Make an array list in your adapter. Its a common and good approach! – Xenolion Jul 14 '18 at 11:19
  • **[This](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is a recommended way in which you can retrieve data from a Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. – Alex Mamo Jul 15 '18 at 13:18

1 Answers1

0

Do this in your main activity..!

Adapter adapter = null;
ArrayList<GetPatients> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_data_list);
    recyclerView = findViewById(R.id.recycler_view);
    adapter = new Adapter(list);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setHasFixedSize(true);
    recyclerView.setAdapter(adapter);
    FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
    DatabaseReference reference = firebaseDatabase.getReference("Patients");
    reference.addChildEventListener(this);
}

@Override
public void onChildAdded(DataSnapshot dataSnapshot,String s) {
    GetPatients value = dataSnapshot.getValue(GetPatients.class);
    list.add(new GetPatients(value.getPatientName(),value.getBloodGroup(),value.getContactNo()));
    adapter.notifyDataSetChanged();
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot,String s) {

}

@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}

}