我有一些字串,我在我的第一個活動中寫入 Edittext,我試圖通過按 Button 將它發送到另一個活動的 Edittext,但我的代碼不起作用,沒有任何反應,我不知道我還能在這里做什么也許有人可以幫助我
我在第一個活動的 onClick 方法:
public void calendarOR(View v, AdapterView<?> parent, int position, long id) {
String data = cala.getText().toString();
Intent intent = new Intent(getApplicationContext(), calendar.class);
intent.putExtra("id",data);
startActivity(intent);
我的第二個活動代碼:
Bundle extras = getIntent().getExtras();
if (extras != null) {
userId = extras.getLong("id");
}
if (userId > 0) {
userCursor = db.rawQuery("select * from " dbelper.TABLE " where " dbelper.COLUMN_ID "=?", new String[]{String.valueOf(userId)});
userCursor.moveToFirst();
date.setText(userCursor.getString(1));
userCursor.close();
}
uj5u.com熱心網友回復:
您正在放置String資料型別并嘗試獲取long. 更改第二個活動以獲取字串形式的“id”。
userId = extras.getString("id");
然后
if (userId != null) {
userCursor = db.rawQuery("select * from " dbelper.TABLE " where " dbelper.COLUMN_ID "=?", new String[]{userId});
userCursor.moveToFirst();
date.setText(userCursor.getString(1));
userCursor.close();
}
如果 userId 為空或資料不在資料庫中,您可能還想列印日志訊息以幫助除錯。
不相關,但您可能希望更改您的意圖,不使用應用程式背景關系,像這樣,來啟動calendarActivity。
Intent intent = new Intent(this, calendar.class);
uj5u.com熱心網友回復:
在第二個活動代碼中執行此操作:
String data =getIntent().getStringExtra("id");
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/392762.html
