2012-04-11 38 views
0

我只是想知道如何讓我的專欄 「狀態」之一的列值,以便將它與字符串「C」比較和「S」,所以各 頁面可以顯示.... !!!要存儲的列值與一些字符串

boolean validLogin = validateLogin(username, password, getApplicationContext()); 
    if(validLogin) 
    { 
     if(status=="s") 

    Intent in = new Intent(getBaseContext(), JSHomePage.class); 
    in.putExtra("UserName", muname.getText().toString()); 
    startActivity(in); 
     } 
    else if(status=="c") 
     { Intent in = new Intent(getBaseContext(), CompView.class); 
     in.putExtra("UserName", muname.getText().toString()); 
     startActivity(in);} 
    } 

我有一個表名登錄其中有3場useranme,密碼和狀態.. !! 這裏編輯的代碼

public void onClick(View v) 
{ 
switch(v.getId()) 
{ 

case R.id.register: 
    Intent i = new Intent(getBaseContext(), Registration.class); 
    startActivity(i); 
    break; 

case R.id.login: 

    muname = (EditText)findViewById(R.id.Ledituname); 
    mpassword = (EditText)findViewById(R.id.Leditpw); 

    String username = muname.getText().toString(); 
    String password = mpassword.getText().toString(); 



    if(username.equals("") || username == null) 
    { 
    Toast.makeText(getApplicationContext(), "Please enter User Name", Toast.LENGTH_SHORT).show(); 
    } 
    else if(password.equals("") || password == null) 
    { 
    Toast.makeText(getApplicationContext(), "Please enter your Password", Toast.LENGTH_SHORT).show(); 
    } 
    else 
    { 
    boolean validLogin = validateLogin(username, password, getApplicationContext()); 

    if(validLogin) 
    {   
     if(status.equals("s")) 
     { 
    Intent in = new Intent(getBaseContext(), JSHomePage.class); 
    in.putExtra("UserName", muname.getText().toString()); 
    startActivity(in); 
     } 
     else if(status.equals("c")) 
     { Intent in = new Intent(getBaseContext(), CompView.class); 
     in.putExtra("UserName", muname.getText().toString()); 
     startActivity(in);} 
    } 
    } 
    break; 

} 

} 


private boolean validateLogin(String username, String password, Context baseContext) 
{ 
    DB = new DBHelper(getBaseContext()); 
    SQLiteDatabase db = DB.getReadableDatabase(); 

    String[] columns = {"_id","status"}; 

    String selection = "username=? AND password=?"; 
    String[] selectionArgs = {username,password}; 

    Cursor cursor = null; 
    try{ 

    cursor = db.query(DBHelper.Login_Table, columns, selection, selectionArgs, null, null, null); 

    startManagingCursor(cursor); 

    } 
    catch(Exception e) 

    { 
    e.printStackTrace(); 
    } 
int numberOfRows = cursor.getCount(); 

    if(numberOfRows <= 0) 
    { 

    Toast.makeText(getApplicationContext(), "User Name and Password miss match..\nPlease Try Again", Toast.LENGTH_LONG).show(); 
    Intent intent = new Intent(getBaseContext(), Login.class); 
    startActivity(intent); 
    return false; 
    } 
    status = cursor.getString(cursor.getColumnIndexOrThrow("status")); 

    return true; 

} 

} 

新的logcat

04-12 20:33:54.169: E/AndroidRuntime(723): FATAL EXCEPTION: main 
04-12 20:33:54.169: E/AndroidRuntime(723): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1 
04-12 20:33:54.169: E/AndroidRuntime(723): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580) 
04-12 20:33:54.169: E/AndroidRuntime(723): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214) 
04-12 20:33:54.169: E/AndroidRuntime(723): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41) 
04-12 20:33:54.169: E/AndroidRuntime(723): at com.google.data.Login.validateLogin(Login.java:139) 
04-12 20:33:54.169: E/AndroidRuntime(723): at com.google.data.Login.onClick(Login.java:83) 
04-12 20:33:54.169: E/AndroidRuntime(723): at android.view.View.performClick(View.java:2408) 
04-12 20:33:54.169: E/AndroidRuntime(723): at android.view.View$PerformClick.run(View.java:8816) 
04-12 20:33:54.169: E/AndroidRuntime(723): at android.os.Handler.handleCallback(Handler.java:587) 
04-12 20:33:54.169: E/AndroidRuntime(723): at android.os.Handler.dispatchMessage(Handler.java:92) 
04-12 20:33:54.169: E/AndroidRuntime(723): at android.os.Looper.loop(Looper.java:123) 
04-12 20:33:54.169: E/AndroidRuntime(723): at android.app.ActivityThread.main(ActivityThread.java:4627) 
04-12 20:33:54.169: E/AndroidRuntime(723): at java.lang.reflect.Method.invokeNative(Native Method) 
04-12 20:33:54.169: E/AndroidRuntime(723): at java.lang.reflect.Method.invoke(Method.java:521) 
04-12 20:33:54.169: E/AndroidRuntime(723): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
04-12 20:33:54.169: E/AndroidRuntime(723): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
04-12 20:33:54.169: E/AndroidRuntime(723): at dalvik.system.NativeStart.main(Native Method) 
+0

所以EQ uals或== – Selvin 2012-04-11 22:52:50

+0

更改'username.equals(「」)||中的順序username == null',否則你會得到NPE以防'username == null' – Vlad 2012-04-11 23:52:30

+0

@vlad:它的工作正常。 。 :-) – sambot 2012-04-12 07:55:28

回答

0

你可以試試這個:

創建一個類變量MSTATUS:

public String mStatus; 

添加「STATUS」你列的排列:

String[] columns = {"_id", "status"}; 

而就在你的validateLogin方法返回true之前,把狀態值在MSTATUS類變量。

mStatus = cursor.getString(cursor.getColumnIndexOrThrow("status")); 

請注意,你的代碼是不是我們可以稱之爲一個「乾淨的代碼」,讓我給你一個可行的解決方案不是一個「乾淨的解決方案」我讓你提高和清潔類;-)

一兩件事,爲了比較的字符串值,你必須使用myString.equals("myvalue")而不是myString == "myvalue"

最後你比較MSTATUS以「S」和「C」

if(validLogin) 
    { 
     if(mStatus=="s") 
     {//System.out.println("In Valid"); 
.... 
+0

嘿thanxs。 。 .m新的Android所以。 。 .dnt很多。 。 。 。我已經完成了你的寫作,但現在我的AVD正在休息。 。爲什麼我必須聲明字符串列。 – sambot 2012-04-12 08:02:03

+0

當您的avd關機時,您是否可以複製您的Logcat的內容?你必須聲明你想從數據庫中檢索的所有列。 – chip 2012-04-12 09:47:17

+0

我編輯了代碼檢查logcat.and也做了我的狀態= cursor.getString(cursor.getColumnIndexOrThrow(「status」)); 在正確的地方? – sambot 2012-04-12 13:40:06