gpt4 book ai didi

java - Firebase - 重复插入数据库

转载 作者:太空宇宙 更新时间:2023-11-04 11:47:38 24 4
gpt4 key购买 nike

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

问题是用户可以多次单击该按钮,它会创建重复的用户,我尝试在他单击该按钮时禁用该按钮,并在创建新用户后启用该按钮,但它不起作用..

我的代码:

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 是一个 Map,只要我的数据库中的/Users 发生变化,它就会不断更新

编辑:我已经编辑了 ValueEventListener,以便每当数据库更新时它都会删除重复项,它会删除旧的重复项,但不会删除新的重复项:

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

最佳答案

您可以为此用户选择一个 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);

}
}
});

关于java - Firebase - 重复插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42180095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com