我在我的数据库类中有这个方法,如下所示:
public HashMap<String, String> getUser() {
HashMap<String, String> user = new HashMap<String, String>();
String selectQuery = "SELECT * FROM " + user;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToFirst();
if (cursor.getCount() > 0) {
user.put("name", cursor.getString(1));
user.put("email", cursor.getString(2));
user.put("userid", cursor.getString(3));
}
cursor.close();
db.close();
return user;
}
在我的homeActivity中,我可以获取如下所示的值:
db = new DatabaseHandler(this);
HashMap<String, String> userdetails = db.getUser();
String name = userdetails.get("name");
userID = userdetails.get("userid");
username.setText(userID + " " + name );
当点击一个继续按钮时,它将转到下一个活动,我想将userid值传递给下一个活动
buttonContinue.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent afterhome = new Intent(getApplicationContext(),
SecondActivity.class);
afterhome.putExtra("userid", userid);
startActivity(afterhome);
}
});
但是,我从secondActivity得到的值总是为0.
我的第二个活动代码如下:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent intent = getIntent();
userid = intent.getStringExtra("userid");
}
为什么会这样?请指教。谢谢