0

我只想知道如何获取我的列“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)
4

1 回答 1

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");
....
于 2012-04-11T23:21:59.450 回答