2013-04-22 56 views
1

我希望當用戶在我的應用程序中註冊用戶名時,如果用戶名已被佔用,他/她將得到一個表示「用戶名已被佔用」的敬酒。如何在註冊活動中防止重複輸入

我已經在我的數據庫中包含了UNIQUE約束,它工作正常,但是在活動本身中我想讓用戶知道他們正在嘗試註冊的用戶名已經存在。

package com.fullfrontalgames.numberfighter; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class Register extends Activity { 
    EditText UsernameReg, PasswordReg, PasswordConfirm, EmailReg; 

    Button Register; 

    DBAdapter db; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.register); 

     // get Instance of Database Adapter 
     final DBAdapter db = new DBAdapter(this); 
     db.open(); 

     // Get reference of views. 
     UsernameReg = (EditText)findViewById(R.id.UsernameReg); 
     PasswordReg = (EditText)findViewById(R.id.PasswordReg); 
     EmailReg = (EditText)findViewById(R.id.EmailReg); 
     PasswordConfirm = (EditText)findViewById(R.id.PasswordConfirm); 

     Register = (Button)findViewById(R.id.Register); 
     Register.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       String Username = UsernameReg.getText().toString(); 
       String Password = PasswordReg.getText().toString(); 
       String PassConfirm = PasswordConfirm.getText().toString(); 
       String Email = EmailReg.getText().toString(); 
       String TakenUsername = db.getData(); 

       // Check if any of the field are vacant. 
       if (Username.equals("") || Password.equals("") || PassConfirm.equals("")) { 
        Toast.makeText(getApplicationContext(), "Field Vacant", Toast.LENGTH_LONG) 
          .show(); 
        return; 
       } 
       // Check if both passwords matches 
       if (!Password.equals(PassConfirm)) { 
        Toast.makeText(getApplicationContext(), "Password does not match", 
          Toast.LENGTH_LONG).show(); 
        return; 
       } 
       if (Username.equals(TakenUsername)) 

       { 
        Toast.makeText(getApplicationContext(), "Username Already Taken", 
          Toast.LENGTH_LONG).show(); 
        return; 
       } else { 
        // Save data in database 
        db.insertPlayer(Username, Password, Email); 
        Toast.makeText(getApplicationContext(), "Account Successfully Created ", 
          Toast.LENGTH_LONG).show(); 
        startActivity(new Intent("com.fullfrontalgames.numberfighter.Login")); 
       } 
      } 

     }); 

    } 

} 
+1

問題出在哪裏? – stinepike 2013-04-22 03:01:42

+0

@StinePike使用我試圖使用的方法時,我註冊一個重複的用戶名,它仍然作用於我的else語句重定向到登錄活動時,我想第二if語句不去登錄活動,並告訴用戶用戶名已經存在才能生效。我知道我使用的方法不正確(如果(Username.equals(TakenUsername))我發佈了這個問題,詢問什麼纔是正確的方法。 – Cranosaur 2013-04-22 03:06:29

+0

chech在數據庫中用戶名的可用性,I我認爲這是更好的解決方案 – stinepike 2013-04-22 03:09:06

回答

2

你可以做到這一點

long id = db.insertPlayer(Username, Password, Email); 
if (id == -1) 
{ 
    Toast.makeText(getApplicationContext(), "Username Already Taken", 
         Toast.LENGTH_LONG).show(); 
} 

其中

public long insertPlayer(String Username, String Password, String Email) 
{ 
    ContentValues values = new ContentValues(); 
    values.put("USERNAME", Username); 
    values.put("PASSWORD", Password); 
    values.put("EMAIL", Email); 

    return db.insertWithOnConflict(Table name, null, values, SQLiteDatabase.CONFLICT_IGNORE); 

} 
+0

這工作完全謝謝你!我從昨晚解決了這個問題,謝謝你給我的BasAdapter例子! – Cranosaur 2013-04-22 03:41:54

+0

這太好了,我還在想如何findViewById返回null。 – 2013-04-22 03:44:11

+0

我有充氣器作爲r.id.listitems而不是r.id.adminlistitems完全忽略了一個大聲笑 – Cranosaur 2013-04-22 03:52:38

0

檢查數據庫表是否存在用戶名。你可以做相反的

if (Username.equals(TakenUsername)) 

      { 
       Toast.makeText(getApplicationContext(), "Username Already Taken", 
         Toast.LENGTH_LONG).show(); 
       return; 
      } else { 
       // Save data in database 
       db.insertPlayer(Username, Password, Email); 
       Toast.makeText(getApplicationContext(), "Account Successfully Created ", 
         Toast.LENGTH_LONG).show(); 
       startActivity(new Intent("com.fullfrontalgames.numberfighter.Login")); 
      } 

以下

Cursor c = db.query(TABLE_NAME, new String[] { USERNAME}, 
     USERNAME+ " ='" + userName + "'", null, null, null, null); 
     if(c.getCount > 0) 
       username taken;;