我只是想知道如何获取我的一个列的列值 “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个字段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 :(得分: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");
....