当返回android时,将结果表格当前活动传递给第一个活动

时间:2011-08-22 06:51:55

标签: android

我有两个类NoteEdit和NoteView,在NoteView类中我按下编辑按钮它将我引导到NoteEidt类,假设我想将我更新的正文信息传递给NoteView,并且之前的正文信息应该已在NoteView界面中更新,但它仍然保持不变,这是我的代码的一部分,请帮助,谢谢!

NoteEdit class:

     Intent resultIntent = new Intent(NoteEdit.this, NoteView.class);
           String body = mBodyText.getText().toString()  
         resultIntent.putExtra(body, true);   

NoteView类

      public boolean onOptionsItemSelected(MenuItem item) {

          switch (item.getItemId()) {
    case DIARY_EDIT:
         Intent i = new Intent(NoteView.this, NoteEdit.class);
         i.putExtra(NotesDbAdapter.KEY_ROWID, mRowId);
         startActivityForResult(i, ACTIVITY_VIEWERS);
         break;
          }  }

         @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        switch(requestCode) {   
        case (ACTIVITY_VIEWERS) 
        : {      
            if (resultCode == Activity.RESULT_OK)
            {    
                Bundle extras = getIntent().getExtras();
                 newText =extras.getString(NotesDbAdapter.KEY_BODY);  
                // TODO Update your TextView.   
                }       
            break;    
            }    
        }  


    }   

4 个答案:

答案 0 :(得分:1)

现在使用此代码。

NoteEdit class:

Intent resultIntent = new Intent(NoteEdit.this, NoteView.class);
       String body = mBodyText.getText().toString()  
     resultIntent.putExtra(NotesDbAdapter.KEY_BODY, body); 
     setResult(RESULT_OK,resultIntent);

NoteView类:

public boolean onOptionsItemSelected(MenuItem item) {

      switch (item.getItemId()) {
case DIARY_EDIT:
     Intent i = new Intent(NoteView.this, NoteEdit.class);
     i.putExtra(NotesDbAdapter.KEY_ROWID, mRowId);
     startActivityForResult(i, ACTIVITY_VIEWERS);
     break;
      }  }

     @Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    switch(requestCode) {   
    case ACTIVITY_VIEWERS 
    : {      
        if (resultCode == RESULT_OK)
        {    
            Bundle extras = intent().getExtras();
             newText =extras.getString(NotesDbAdapter.KEY_BODY);  
            // TODO Update your TextView.   
            }       
        break;    
        }    
    }  


}   

答案 1 :(得分:0)

setResult(Activity.RESULT_OK, resultIntent)中使用NodeEdit 并参见http://developer.android.com/reference/android/app/Activity.html#StartingActivities
修改
您还应该使用resultIntent.putExtra(NotesDbAdapter.KEY_BODY, body);中的NodeEdit将字符串值body传递给另一个活动。并使用extras.getString(NotesDbAdapter.KEY_BODY);

获取它

答案 2 :(得分:0)

尝试这样的事情。

Intent resultIntent = getIntent();
String body = mBodyText.getText().toString()  
resultIntent.putExtra(body, true);
setResult(RESULT_OK,resultIntent);
finish();

别忘了拨打finish()

答案 3 :(得分:0)

NoteEdit class:

//write this code when edit finished
Intent resultIntent = new Intent(getIntent()); // Note this line is important!
String body = mBodyText.getText().toString()
resultIntent.putExtra(NotesDbAdapter.KEY_BODY, body); setResult(RESULT_OK,resultIntent); finish();

NoteView类:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case DIARY_EDIT:
        Intent i = new Intent(NoteView.this, NoteEdit.class);
        i.putExtra(NotesDbAdapter.KEY_ROWID, mRowId);
        startActivityForResult(i, ACTIVITY_VIEWERS);
        break;
    }
}

@Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); switch(requestCode) { case ACTIVITY_VIEWERS: { if (resultCode == RESULT_OK) { Bundle extras = intent().getExtras(); newText =extras.getString(NotesDbAdapter.KEY_BODY); // TODO Update your TextView. } break; } } }