2014-02-08 41 views
1

我的變量如下聲明:更新的TextView顯示用戶輸入的文本,Intent.getStringExtra()

static private final int GET_TEXT_REQUEST_CODE = 1; 
     static private final String URL = "http://www.google.com"; 
     static private final String TAG = "Lab-Intents"; 


     static private final String CHOOSER_TEXT = "Load " + URL + " with:"; 


     private TextView mUserTextView; 

我不知道如何在onActivityResult完成代碼()方法。我不確定是否更新了正確顯示用戶輸入文本的Textview。我必須使用Intent.getStringExtra()。我用它,但作爲getIntent()。getStringExtra(「mUserTextView」);

private void startImplicitActivation() { 

     Log.i(TAG, "Entered startImplicitActivation()"); 



       Uri url = Uri.parse(URL);    
       Intent baseIntent = new Intent(Intent.ACTION_VIEW, url); 
      Intent chooserIntent = Intent.createChooser(baseIntent, CHOOSER_TEXT); 
      Log.i(TAG,"Chooser Intent Action:" + chooserIntent.getAction()); 

      startActivity(chooserIntent); 

    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    Log.i(TAG, "Entered onActivityResult()"); 

    // TODO - Process the result only if this method received both a 
    // RESULT_OK result code and a recognized request code 
    // If so, update the Textview showing the user-entered text. 

if (requestCode == GET_TEXT_REQUEST_CODE){ 
    if (requestCode == RESULT_OK){ 
    getIntent().getStringExtra("mUserTextView");  
    } 
} 

} 
} 

回答

2

要更新的標籤,你應該使用:

if (resultCode == RESULT_OK && requestCode == GET_TEXT_REQUEST_CODE) { 
    mUserTextView.setText(data.getStringExtra("resultado")); 
} 

別忘了撥打 「ExplicitlyLoadedActivity」,與請求代碼GET_TEXT_REQUEST_CODE:

Intent myIntent = new Intent(ActivityLoaderActivity.this, ExplicitlyLoadedActivity.class); 
startActivityForResult(myIntent, GET_TEXT_REQUEST_CODE); // just to be sure about Request code 

更改單詞 「resultado」對於你在enterClicked()中用於ExplicitlyLoadedActivity的那個

String givenText = mEditText.getText().toString(); // Getting the user input 
Intent intencion = new Intent(); // Getting ready to comeback 
intencion.putExtra("resultado", givenText); // I use "resultado", spanish guy 
setResult(RESULT_OK, intencion); // RESULT_OK is a constant = 1 
finish(); 

祝你好運寬度coursera實驗室,我也這樣做。乾杯

+0

非常感謝,但startActivityForResult(intent,GET_TEXT_REQUEST_CODE);給我一個錯誤。當我像這樣寫startActivityForResult(getIntent(),GET_TEXT_REQUEST_CODE)時就可以了; – user3284769

+0

我編輯了答案,檢查它是否適合你。在意圖,請確保您使用的活動名稱 – ibpeco

+0

謝謝!祝你好運 – user3284769

0

Intent類型的靜態方法createChooser(Intent, CharSequence)應該以靜態方式訪問。

unfortunately the project has stopped 
0

chooserIntent爲空:這是當我在chooserIntent.createChooser(map,CHOOSER_TEXT);

// CODE

private void startImplicitActivation() 
{ 
    Log.i(TAG, "Entered startImplicitActivation()"); 

    // TODO - Create a base intent for viewing a URL 
    // (HINT: second parameter uses parse() from the Uri class) 
    Intent map = new Intent(Intent.ACTION_VIEW,Uri.parse(URL)); 


    // TODO - Create a chooser intent, for choosing which Activity 
    // will carry out the baseIntent. Store the Intent in the 
    // chooserIntent variable below. HINT: using the Intent class' 
    // createChooser()) 


    Intent chooserIntent=null; 
    chooserIntent.createChooser(map,CHOOSER_TEXT); 

    Log.i(TAG,"Chooser Intent Action:" + chooserIntent.getAction()); 
    // TODO - Start the chooser Activity, using the chooser intent 
    startActivity(chooserIntent); 

} 

這會導致當我運行的應用程序的錯誤運行相同的代碼所示。首先創建它: Intent chooserIntent = Intent.createChooser(map,CHOOSER_TEXT);