Firebase - 重复插入数据库

时间:2017-02-11 19:11:43

标签: java android firebase

我有一个带有注册表单的活动,当用户成功注册它时,会在我的数据库中创建一个新子项并启动登录活动。

问题是用户可以多次点击该按钮并创建重复的用户,我尝试在点击该按钮时禁用该按钮,并在创建新用户后启用该按钮,但它没有工作..

我的代码:

register_btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final String username = txt_username.getText().toString().trim();
        final String password = txt_password.getText().toString().trim();
        final String email = txt_email.getText().toString();

        register_btn.setEnabled(false);

        if(username.length() < 3) {
            Toast.makeText(RegisterActivity.this, "Username must be atleast 3 characters long!", Toast.LENGTH_SHORT).show();
            register_btn.setEnabled(true);
            return;
        }
        if(password.length() < 6) {
            Toast.makeText(RegisterActivity.this, "Password must be atleast 6 characters long!", Toast.LENGTH_SHORT).show();
            register_btn.setEnabled(true);
            return;
        }

        if(!validateEmail(email)) {
            Toast.makeText(RegisterActivity.this, "The email entered is invalid", Toast.LENGTH_SHORT).show();
            register_btn.setEnabled(true);
            return;
        }

        for(Map.Entry<String, Object> e : users.entrySet()) {
            Map<String, Object> user = (HashMap<String, Object>) e.getValue();

            if(user.get("username").toString().equalsIgnoreCase(username)) {
                Toast.makeText(RegisterActivity.this, "This username already exists", Toast.LENGTH_SHORT).show();
                register_btn.setEnabled(true);
                return;
            }

            if(user.get("email").toString().equalsIgnoreCase(email)) {
                Toast.makeText(RegisterActivity.this, "This email already exists", Toast.LENGTH_SHORT).show();
                register_btn.setEnabled(true);
                return;
            }

            final String uid = randomUID();
            final int id = users.size() + 1;

            Map<String, Object> newUser = createUser(id, username, password, email);

            ref.child(uid).setValue(newUser);

            Toast.makeText(RegisterActivity.this, "Successfully registered!", Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
            startActivity(intent);

            register_btn.setEnabled(true);

        }
    }
});

用户是每当我的数据库中的/ Users更改

时不断更新的地图

编辑:我编辑了ValueEventListener,因此每当数据库更新时它都会删除重复项,它会删除旧的重复项,但它会删除新的重复项&#39; nt:< / p>

ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        users = (HashMap<String,Object>) dataSnapshot.getValue();
        Map<String, Object> last_user = null;

        for(Map.Entry<String, Object> e : users.entrySet()) {
            Map<String, Object> user = (HashMap<String, Object>) e.getValue();
            if(last_user == null) {
                last_user = user;
                continue;
            }
            if(user.get("username").toString().equalsIgnoreCase(last_user.get("username").toString())) {
                int id = Integer.parseInt(user.get("id").toString());
                int lid = Integer.parseInt(last_user.get("id").toString());
                if(id < lid || id == lid) {
                    FirebaseDatabase.getInstance().getReference("Users/" + e.getKey()).removeValue();
                }
            }

            last_user = user;

        }

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Toast.makeText(RegisterActivity.this, "The following error occured", Toast.LENGTH_SHORT).show();
        Toast.makeText(RegisterActivity.this, databaseError.toString(), Toast.LENGTH_LONG).show();
    }
});

1 个答案:

答案 0 :(得分:0)

您可以为此用户选择一个uid。因此,如果他再次点击该按钮,Firebase将不会创建其他用户,而是会更新其值。

final String uid = ref.push().getKey(); //OR call for your randomuid() methodd if you prefer
register_btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final String username = txt_username.getText().toString().trim();
        final String password = txt_password.getText().toString().trim();
        final String email = txt_email.getText().toString();

        register_btn.setEnabled(false);

        if(username.length() < 3) {
            Toast.makeText(RegisterActivity.this, "Username must be atleast 3 characters long!", Toast.LENGTH_SHORT).show();
            register_btn.setEnabled(true);
            return;
        }
        if(password.length() < 6) {
            Toast.makeText(RegisterActivity.this, "Password must be atleast 6 characters long!", Toast.LENGTH_SHORT).show();
            register_btn.setEnabled(true);
            return;
        }

        if(!validateEmail(email)) {
            Toast.makeText(RegisterActivity.this, "The email entered is invalid", Toast.LENGTH_SHORT).show();
            register_btn.setEnabled(true);
            return;
        }

        for(Map.Entry<String, Object> e : users.entrySet()) {
            Map<String, Object> user = (HashMap<String, Object>) e.getValue();

            if(user.get("username").toString().equalsIgnoreCase(username)) {
                Toast.makeText(RegisterActivity.this, "This username already exists", Toast.LENGTH_SHORT).show();
                register_btn.setEnabled(true);
                return;
            }

            if(user.get("email").toString().equalsIgnoreCase(email)) {
                Toast.makeText(RegisterActivity.this, "This email already exists", Toast.LENGTH_SHORT).show();
                register_btn.setEnabled(true);
                return;
            }

            final int id = users.size() + 1;

            Map<String, Object> newUser = createUser(id, username, password, email);

            ref.child(uid).setValue(newUser);

            Toast.makeText(RegisterActivity.this, "Successfully registered!", Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
            startActivity(intent);

            register_btn.setEnabled(true);

        }
    }
});