如何获取附加到另一个活动的游标数据并拆分附加数据以获取该行的Id

时间:2015-11-27 09:45:52

标签: android sqlite

我有一个数据库,其中我有唯一ID,电子邮件ID和密码。我使用SQlite数据库存储。我要获取存储查询结果的光标,我有每个cloumn的cloumn索引,使用StringBuffer附加它,但我不知道如何在其他类中获取这些值?请帮忙。 这是我的Adapter类的代码:

public String getData(String email,String pwd)
{
    StringBuffer buffer=new StringBuffer();
    SQLiteDatabase db =sciHelper.getWritableDatabase();

    Cursor cursor = db.rawQuery("SELECT _id FROM " + SciHelper.TABLE_NAME + " WHERE email=? AND password=?", new String[]{email, pwd});
    while(cursor.moveToNext())
    {
        int index1=cursor.getColumnIndex(SciHelper.UID);
        int index2=cursor.getColumnIndex(SciHelper.EMAIL);
        int index3=cursor.getColumnIndex(SciHelper.PASSWORD);
        String cid=cursor.getString(index1);
        String mail=cursor.getString(index2);
        String mailpass=cursor.getString(index3);
        buffer.append(cid +" "+mail+" "+mailpass+"\n");
     }
   return buffer.toString();
}

登录类代码:

public void checkTable(View view) {
    email2 = emaillog.getText().toString();
    pass2 = passlog.getText().toString();
    String data = sciDataBaseAdapter.getData(email2, pass2);
    String[] values=data.split("\\");

    String cid=values(0);
    if (TextUtils.isEmpty(email2)) {
        emaillog.setError("Enter Email Id");
        passlog.setError("Enter Password");
    }

        //   if (email1 != null) {
        Intent intent=new Intent(this,ResultActivity.class);
        startActivity(intent);
        Message.message(this, "Login succesful");
    } else {
        Message.message(this, "Invalid username/ or register");
    }
  }
}

2 个答案:

答案 0 :(得分:1)

调用ResultAcitvity

Intent intent=new Intent(this,ResultActivity.class);
intent.putExtra("key","value");
startActivity(intent);

并在ResultActivity上,在 onCreate()方法中写入

Intent intent=getIntent();
String value=intent.getStringExtra("key");
String[] values = value.split(" ");
String cid=values[0];
String mail=values[1];
String mailpass=values[2];

答案 1 :(得分:0)

public void checkTable(View view) {
email2 = emaillog.getText().toString();
pass2 = passlog.getText().toString();
String data = sciDataBaseAdapter.getData(email2, pass2);
String[] values=data.split("\\");

String cid=values(0);
if (TextUtils.isEmpty(email2)) {
    emaillog.setError("Enter Email Id");
    passlog.setError("Enter Password");
}

    //   if (email1 != null) {
 Bundle bundle=new Bundle();
 b.putStringArray(some_key, values);
    Intent intent=new Intent(this,ResultActivity.class);
    intent.putExtra(bundle);
    startActivity(intent);
    Message.message(this, "Login succesful");
} else {
    Message.message(this, "Invalid username/ or register");
}
}

并在您的下一个活动中使用。

Bundle extras = getIntent().getExtras();
String[] some_variable= extras.getString("some_key");