Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

android - visually duplicating existing record when deleted/updated record

does anyone here encountered when you are updating a data it duplicates the existing data visually only but when I will back and go to the activity again it shows the real data. Data 1 and Data 2, when I delete Data 2, it will show Data 1 Data2 and Data 1. However like I said, it is just visual had to go back and go to the activity again to show the current data which is Data 1 only since Data 2 has been deleted. I have tried searching but none of them are related to my problem and I genuinely do not know if this is from the database or the card or in the recyclerview. Any help will be much appreciated.

MainActivity

public class AdminReqFormsActivity extends AppCompatActivity {
    RecyclerView adminreqform_recycler;
    ImageView adminreqfromfield_backbtn;
    DatabaseReference databaseReference;
    ArrayList<ResearchReqForm> reqFormArrayList;
    AdminRequestFormAdapter adminRequestFormAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_req_forms);
        adminreqform_recycler = findViewById(R.id.adminreqform_recycler);
        adminreqfromfield_backbtn = findViewById(R.id.adminreqfromfield_backbtn);


        databaseReference = FirebaseDatabase.getInstance().getReference("ResearchRequest");
        adminreqform_recycler.setLayoutManager(new LinearLayoutManager(this));
        reqFormArrayList = new ArrayList<ResearchReqForm>();
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot dataSnapshot : snapshot.getChildren()){
                    ResearchReqForm researchReqForm = dataSnapshot.getValue(ResearchReqForm.class);
                    reqFormArrayList.add(researchReqForm);
                }
                adminRequestFormAdapter = new AdminRequestFormAdapter(AdminReqFormsActivity.this, reqFormArrayList);
                adminreqform_recycler.setAdapter(adminRequestFormAdapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Toast.makeText(AdminReqFormsActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
        adminreqfromfield_backbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), WelcomeAdminActivity.class));
            }
        });
    }
}

Model

public class ResearchReqForm {
    String studentName;
    String requestedtitle;
    String requestedtags;
    String requestMessage;
    String requestid;
    String requestStatus;

    public ResearchReqForm() {
    }

    public ResearchReqForm(String studentName, String requestedtitle, String requestedtags, String requestMessage, String requestid, String requestStatus) {
        this.studentName = studentName;
        this.requestedtitle = requestedtitle;
        this.requestedtags = requestedtags;
        this.requestMessage = requestMessage;
        this.requestid = requestid;
        this.requestStatus = requestStatus;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public String getRequestedtitle() {
        return requestedtitle;
    }

    public void setRequestedtitle(String requestedtitle) {
        this.requestedtitle = requestedtitle;
    }

    public String getRequestedtags() {
        return requestedtags;
    }

    public void setRequestedtags(String requestedtags) {
        this.requestedtags = requestedtags;
    }

    public String getRequestMessage() {
        return requestMessage;
    }

    public void setRequestMessage(String requestMessage) {
        this.requestMessage = requestMessage;
    }

    public String getRequestid() {
        return requestid;
    }

    public void setRequestid(String requestid) {
        this.requestid = requestid;
    }

    public String getRequestStatus() {
        return requestStatus;
    }

    public void setRequestStatus(String requestStatus) {
        this.requestStatus = requestStatus;
    }
}

Adapter

public class AdminRequestFormAdapter extends RecyclerView.Adapter<AdminRequestFormAdapter.AdminRequestFormViewHolder> {
    Context context;
    ArrayList<ResearchReqForm> requestFormArrayList;

    public AdminRequestFormAdapter(Context c, ArrayList<ResearchReqForm> reqFormsList){
        context = c;
        requestFormArrayList = reqFormsList;
    }
    @NonNull
    @Override
    public AdminRequestFormViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AdminRequestFormAdapter.AdminRequestFormViewHolder(LayoutInflater.from(context).inflate(R.layout.admin_request_list,parent,false));

    }

    @Override
    public void onBindViewHolder(@NonNull AdminRequestFormViewHolder holder, final int position) {
        holder.adminsudentname_reqcv.setText(requestFormArrayList.get(position).getStudentName());
        holder.adminrelatedtopic_requestcv.setText(requestFormArrayList.get(position).getRequestedtitle());
        holder.admintopictags_requestcv.setText(requestFormArrayList.get(position).getRequestedtags());
        holder.adminbriefmessage_reqeuestcv.setText(requestFormArrayList.get(position).getRequestMessage());
        holder.deleteresearch_request.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alert = new AlertDialog.Builder(context);
                alert.setTitle("Delete Request Research?");
                alert.setMessage("Are you sure you want to delete?");

                alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("ResearchRequest");
                        final String uniqueKey = requestFormArrayList.get(position).getRequestid();
                        databaseReference.child(uniqueKey).removeValue();
                        Toast.makeText(context, "Student's Request has been deleted.", Toast.LENGTH_SHORT).show();

                    }
                });
                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "Form Closed.", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                });
                alert.show();
            }
        });
        holder.acceptresearch_request.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "Test Toast For Accept", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return requestFormArrayList.size();
    }

    class AdminRequestFormViewHolder extends RecyclerView.ViewHolder{
        TextView adminsudentname_reqcv, adminrelatedtopic_requestcv, admintopictags_requestcv, adminbriefmessage_reqeuestcv;
        Button deleteresearch_request, acceptresearch_request;
        public AdminRequestFormViewHolder(@NonNull View itemView) {
            super(itemView);
            adminsudentname_reqcv = itemView.findViewById(R.id.adminsudentname_reqcv);
            adminrelatedtopic_requestcv =itemView.findViewById(R.id.adminrelatedtopic_requestcv);
            admintopictags_requestcv = itemView.findViewById(R.id.admintopictags_requestcv);
            adminbriefmessage_reqeuestcv = itemView.findViewById(R.id.adminbriefmessage_reqeuestcv);

            deleteresearch_request = itemView.findViewById(R.id.deleteresearch_request);
            acceptresearch_request = itemView.findViewById(R.id.acceptresearch_request);
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, this is actually quite common and comes from a misunderstanding of how Firebase snapshots work.

Whenever your onDataChange is called it gets a full snapshot of all the data at databaseReference. So even if there's only one change since you rendered the data, you get called with a full snapshot of all data at databaseReference including the change. And since you add all data in snapshot to reqFormArrayList, you end up duplicating it each time onDataChange gets called.

The simplest solution is to clear reqFormArrayList inside onDataChange:

databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        reqFormArrayList.clear(); // this is the new line
        for (DataSnapshot dataSnapshot : snapshot.getChildren()){
            ResearchReqForm researchReqForm = dataSnapshot.getValue(ResearchReqForm.class);
            reqFormArrayList.add(researchReqForm);
        }
        adminRequestFormAdapter = new AdminRequestFormAdapter(AdminReqFormsActivity.this, reqFormArrayList);
        adminreqform_recycler.setAdapter(adminRequestFormAdapter);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        Toast.makeText(AdminReqFormsActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
    }
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...