如何删除Firebase android中的重复条目

时间:2017-11-19 18:43:31

标签: android firebase firebase-realtime-database

我需要消除firebase数据库中的重复条目。

这是数据库

enter image description here

当用户插入数据时需要检查" group_link"提出与否。

  b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mref=FirebaseDatabase.getInstance().getReference().child("type").child(group_type).child(selected_item);
            group_name=e1.getText().toString().trim();
            grou_link=e2.getText().toString().trim();
            language=e3.getText().toString().trim();
            if(!grou_link.isEmpty()&&!group_name.isEmpty()) {
                mref.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        for(DataSnapshot getdata : dataSnapshot.getChildren()){
                            if (getdata.child("group_link").exists()) {
                                Toast.makeText(Usr_add.this,"Stop it exists!!!",Toast.LENGTH_SHORT).show();

                            }}
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
                HashMap<String, String> datamap = new HashMap<String, String>();
                datamap.put("category", selected_item);
                datamap.put("group_name", group_name);
                datamap.put("group_link", grou_link);
                datamap.put("group_type",group_type);
                datamap.put("language",language);
                datamap.put("report_status",count);

                mref.push().setValue(datamap).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(Usr_add.this, "Data Submited successfully :) ", Toast.LENGTH_SHORT).show();
                            e1.setText("");
                            e2.setText("");
                        } else
                            Toast.makeText(Usr_add.this, "Something Wrong check your internet connection :( ", Toast.LENGTH_SHORT).show();
                    }
                });
            }
            else
                Toast.makeText(Usr_add.this,"Error: Please enter all the fields or enter the correct group Link",Toast.LENGTH_LONG).show();

        }
    });

每次用户输入数据时,它都会提供像#34;停止存在的祝酒!&#34;然后添加数据

1 个答案:

答案 0 :(得分:0)

b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mref = FirebaseDatabase.getInstance().getReference().child("type").child(group_type).child(selected_item);

            final boolean[] exists = {false};

            group_name=e1.getText().toString().trim();
            grou_link=e2.getText().toString().trim();
            language=e3.getText().toString().trim();
            if(!grou_link.isEmpty() && !group_name.isEmpty()) {
                mref.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        for(DataSnapshot getdata : dataSnapshot.getChildren()){
                            if (getdata.child("group_link").exists()) {
                                Toast.makeText(Usr_add.this,"Stop it exists!!!",Toast.LENGTH_SHORT).show();
                                exists[0] = true;
                                break;

                            }
                        }

                        if(!exists[0]) {
                            HashMap<String, String> datamap = new HashMap<>();
                            datamap.put("category", selected_item);
                            datamap.put("group_name", group_name);
                            datamap.put("group_link", grou_link);
                            datamap.put("group_type",group_type);
                            datamap.put("language",language);
                            datamap.put("report_status",count);

                            mref.push().setValue(datamap).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if (task.isSuccessful()) {
                                        Toast.makeText(Usr_add.this, "Data Submited successfully :) ", Toast.LENGTH_SHORT).show();
                                        e1.setText("");
                                        e2.setText("");
                                    } else
                                        Toast.makeText(Usr_add.this, "Something Wrong check your internet connection :( ", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }

                            @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });

                } else
                    Toast.makeText(Usr_add.this,"Error: Please enter all the fields or enter the correct group Link",Toast.LENGTH_LONG).show();

            }
        });