新添加的子数据不会填充新卡视图,它只会更新一张已存在的卡,因此应更改适配器中的内容以填充新卡

时间:2018-07-14 08:05:31

标签: android firebase firebase-realtime-database recycler-adapter android-cardview

因此,当在firebase中添加孩子时,适配器中填充新卡的适配器应进行哪些更改,即通过模态类的对象发送数据。现在,它仅使一张卡上更新了新添加的数据。没有新卡我不想一次又一次地获取全部数据,我只想一次获取数据,添加孩子时应该重新获取数据,因为管理员必须批准患者数据(然后然后应将其删除。)

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

        }
    }
}

我的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>

1 个答案:

答案 0 :(得分:0)

在您的主要活动中执行此操作。.

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

}

}