2011-02-28 107 views
1

驗證我有3 Java文件 我)DBAdapter.java II)signup.java III)login.java 四一個是菜單,但沒有必要在此提及。安卓:如何創建登錄頁面是從數據庫

現在我想這樣做,當用戶來登錄頁面,並在編輯視圖中輸入用戶名和密碼,檢查數據庫中的數據和重定向到菜單,如果發現匹配。 我不知道如何做到這一點,這裏是DBAdapter.java我的代碼文件和login.java

DBAdapter.java

public class DBAdapter 
{ 
    public static final String KEY_ROWID = "_id"; 
    public static final String KEY_FIRSTNAME = "FirstName"; 
    public static final String KEY_LASTNAME = "LastName"; 
    public static final String KEY_USERNAME = "UserName"; 
    public static final String KEY_PASSWORD = "Password";  
    private static final String TAG = "DBAdapter"; 

    private static final String DATABASE_NAME = "master"; 
    private static final String DATABASE_TABLE = "register"; 
    private static final int DATABASE_VERSION = 1; 

    private static final String DATABASE_CREATE = 
     "create table titles (_id integer primary key autoincrement, " 
     + "FirstName text not null, LastName text not null, " 
     + "UserName text not null, Password text not null);"; 

    private final Context context; 

    private DatabaseHelper DBHelper; 
    private SQLiteDatabase db; 

    public DBAdapter(Context ctx) 
    { 
     this.context = ctx; 
     DBHelper = new DatabaseHelper(context); 
    } 

    private static class DatabaseHelper extends SQLiteOpenHelper 
    { 
     DatabaseHelper(Context context) 
     { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) 
     { 
      db.execSQL(DATABASE_CREATE); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, 
     int newVersion) 
     { 
      Log.w(TAG, "Upgrading database from version " + oldVersion 
        + " to " 
        + newVersion + ", which will destroy all old data"); 
      db.execSQL("DROP TABLE IF EXISTS titles"); 
      onCreate(db); 
     } 
    } 

login.java

public class login extends Activity { 

    private EditText username; 
    private EditText password; 

    public String user_name; 
    public String pass_word; 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.login); 

     username = (EditText) findViewById(R.id.ev_unameLogin); 
     password = (EditText) findViewById(R.id.ev_passwordLogin); 

     final Button button = (Button) findViewById(R.id.btn_login); 
     button.setOnClickListener(new View.OnClickListener() {   
      public void onClick(View v) {    

       user_name = username.getText().toString(); 
       pass_word = password.getText().toString(); 

       Intent goToNextActivity = new Intent(getApplicationContext(), menu.class); 
       startActivity(goToNextActivity); 


      } 
     });     


    } 
} 

那麼什麼我應該更新這兩個文件..

非常感謝提前

+0

究竟是什麼問題?你不知道如何完成什麼? – Guy 2011-02-28 09:20:34

+0

que是我如何在數據庫中提取檢索數據(在user_name和pass_word中)。總之是註冊與否。 – Harry 2011-02-28 09:25:33

回答

3

在您需要創建一個方法來訪問數據庫,例如數據庫類。

public boolean validateUser(String username, String password){ 
    Cursor c = getReadableDatabase().rawQuery(
      "SELECT * FROM " + TABLE_NAME + " WHERE " 
        + USER_NAME + "='" + username +"'AND "+PASSWORD+"='"+password+"'" , null); 
    if (c.getCount>0) 
     return true; 
     return false; 
} 

你當然需要創建一些數據庫表像

public void createTable(SQLiteDatabase database) { 
    String creationString = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME 
      + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " 
      + USER_NAME + " TEXT NOT NULL, " 
      + PASSWORD + " TEXT NOT NULL, " 
      ");"; 
    database.execSQL(creationString); 
} 

而且有變量,如TABLE_NAME這顯然是要爲表的名稱,USER_NAME它代表用戶名列的名稱,以及PASSWORD的名稱。

您需要在數據庫的onCreate中調用createTable()方法。

要使用數據庫的登錄按鈕,你可以這樣做:

if (myDatabase.vaidateUser(user_name,pass_word)) { 
    Intent goToNextActivity = new Intent(getApplicationContext(), menu.class); 
    startActivity(goToNextActivity); 

} 
else 
    Toast.makeText(mContext, "Wrong username/password", Toast.LENGTH_LONG).show(); 

希望這有助於。我給了你一個很好的起點。還有其他一些你需要自己去解決的東西,但是這應該會給你一個關於它是如何工作的以及接下來要去哪裏的好主意(比如把記錄添加到數據庫等)。你將需要創建一些變量等,但這並不困難!

+0

我會嘗試這個肯定... – Harry 2011-02-28 12:16:18

+0

嘿夥計我已經收到「getReadableDatabase()」和「getCount」錯誤,當我在我的DBAdapter.java文件中寫入此代碼..所以我必須導入任何包它??? 在此先感謝 – Harry 2011-03-01 04:43:44

+0

修改一點點,即時通訊完成這個邏輯...非常感謝你的回覆 – Harry 2011-03-01 12:20:30

1

public void onClick(View v)方法應該調用一個方法來驗證用戶。

公共無效的onClick(視圖v){

  user_name = username.getText().toString(); 
      pass_word = password.getText().toString(); 
      if(validate(user_name, pasass_word)); 
      {    
       Intent goToNextActivity = new Intent(getApplicationContext(), menu.class); 
      startActivity(goToNextActivity); 
      } 
     } 




public boolean validate(String name,String password) { 
    String url = "http://n.n.n.n:nnnn/WebServicePro/services/Authentication";/*give Ip addres:portnumber*/ 
    String nameSpace="http://com.xxx.yy"; /**give proper namespace*/ 
    String methoName= "authenticate"; /*This method is an operation in the webservice*/ 
    SoapObject request = new SoapObject(nameSpace,methoName); 

    request.addProperty("in0",name); 
    request.addProperty("in1",password); 

    try { 
    resultsRequestSOAP = makeSoapCall(request, "", url); 
     if(resultsRequestSOAP.toString().equalsIgnoreCase("true")){  
     return true; 
      } 
    else{  /*handle error*/} 
    }catch (Exception exp) {    
     /*handle exception*/} 
    return false; 
} 
+1

嘿,我是新手到android,所以我不明白你在做什麼在驗證類(字符串url,命名空間,methoname和肥皂對象) 可以請你解釋我...非常感謝你的迴應 ,並提前感謝這個... – Harry 2011-02-28 09:33:08

+0

這裏是使用webservice驗證user.'authenticate()'是web服務中處理驗證機制的操作。'url'和'namespace'是訪問它所必需的。 – Sora 2011-03-01 06:24:34

+0

所以我要寫在URL和名稱空間部分...有點混淆......感謝您的反應和預先感謝... – Harry 2011-03-01 10:06:14