我只想知道如何获取我的列“STATUS”之一的列值,以便将其与字符串“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 个字段用户名、密码和状态..!!这是编辑的代码
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;
}
}
新的日志猫
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)