2012-05-25 60 views
0

我有兩個活動說A1和A2,他們都有兩個編輯文本字段,一個用於故事的標題和其他。當用戶在活動A1中輸入文本時,在這兩個文本字段中,然後按下保存按鈕,條目將保存在列表視圖中。現在,當用戶再次點擊剛剛保存的條目時,標題和故事應顯示在活動A2的標題和故事文本字段中。我在活動A2中獲得了標題,但我不明白爲什麼故事部分沒有顯示出來。如何顯示兩個文本字段

下面是活動A1的代碼,單擊項目時。

static String title = ""; 
static String story = ""; 

@Override 
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
    // here arg3 is the id of the item which is pressed 
    registerForContextMenu(arg1); 
    id_item_clicked = arg3; 
    position_item = arg2; 
    Cursor c = (Cursor) arg0.getItemAtPosition(arg2); 
    title = c.getString(c.getColumnIndex(DataHolder.KEY_TITLE)); 
    story = c.getString(c.getColumnIndex(DataHolder.KEY_STORY)); 
    Intent i = null; 

    // remove this toast later 
    Toast.makeText(DiaryActivity.this, "TestClick", Toast.LENGTH_SHORT).show(); 

    try { 
     i = new Intent(A1.this, 
       Class.forName("com.android.project.A2")); 
     i.putExtra(ROW_ID, arg3); 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } 

    startActivity(i); 
} 

下面是活動A2,它顯示在兩個文本字段中的文本代碼:

EditText title_read, display_story_read; 
private long rowId; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.readdata); 

    // bundle receives the row id of the item clicked 
    Bundle extras = getIntent().getExtras(); 

    rowId = extras.getLong("row_id"); 

    setupVaiables(); 

    title_read.setText(A1.title); 
    display_story_read.setText(A1.story); 
} 

是越來越顯示的標題,但故事部分沒有,請幫助我,我一直在努力嘗試幾個小時。有人可以告訴我爲什麼我只能回答我的問題嗎?

回答

0

以這種方式傳遞變量是不好的做法。您可以通過意圖傳遞變量:

i.putExtra(TITLE_KEY, title); 
i.putExtra(STORY_KEY, story); 

或在活動A2的數據庫,而不是A1拿到冠軍和故事。

你確定故事字段是寫在數據庫中嗎?檢查你得到的故事的價值,把它寫在日誌或吐司裏。


附註:您可以從類構造意圖:

Intent i = new Intent(A1.this, A2.class); 

在這種情況下,你不需要try..catch塊。