2011-10-22 134 views
0

正如標題所說我要傳輸的數據,在這種情況下,由用戶上EditTextSpinner介紹的信息,從一個活動到另一個。問題從一個活動將數據發送到另一個應用程序

我下面從一本書一個教程,但它不工作(我認爲它不完整)。這裏的程序代碼:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.setContentView(R.layout.main); 
    this.location=(EditText)findViewById(R.id.location); 
    this.cuisine=(Spinner)findViewById(R.id.cuisine); 
    this.grabReviews=(Button)findViewById(R.id.get_reviews_button); 

    ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.cuisine, android.R.layout.simple_spinner_item); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

    this.cuisine.setAdapter(adapter); 
    this.grabReviews.setOnClickListener(new OnClickListener() 
     { 
      public void onClick(View v) 
      { 
       handleGetReviews(); 
      } 
     } 
    ); 
} 

private void handleGetReviews() { 

    RestaurantsActivity application= (RestaurantsActivity) getApplication(); 
    application.setReviewCriteriaCuisine(this.cuisine.getSelectedItem().toString()); 
    application.setReviewCriteriaLocation(this.location.getText().toString()); 
    Intent intent=new Intent(Constants.INTENT_ACTION_VIEW_LIST); 
    startActivity(intent); 

} 

上面的這段代碼不起作用。我不明白四兩件事:

- RestaurantsActivity必須是實際的活動嗎?

- 在我所看到的在互聯網上有一個應用程序的所有例子擴展類,在這個例子中有未啓用。

- setReviewCriteria功能缺失

-where確實來自Constants.INTENT_ACTION_VIEW_LIST

回答

3

所以你的目標是讓數據Restaurantsactivity? 通常情況下,Android中的數據通過使用Intents從一個活動轉移到另一個活動。

因此首先要創建一個意圖。 然後通過使用intent.putExtra()方法將要傳輸的數據放入意圖中。 在獲取意圖的活動中,您可以使用getIntent()。getExtra()方法(getExtra可以像getStringExtra())那樣獲取數據。

這裏是所謂的「名」編輯框的小例子:

public void onCreate(Bundle iBundle){ 
    //do some stuff here 
    //perhaps define some Buttos and so on 

    //now lets start the activity 
    Intent intent = new Intent(currentActivityname.this, ActivityYouWantToStart.class); 
    intent.putExtra("name", name.getText().toString()) 
    startActivity(intent); // you can also start an startActivityForResult() here :) 
} 

在我們接收的活動,你現在可以處理這個意圖(例如,在onCreate()方法

public void onCreate(Bundle iBundle){ 
String name = this.getIntent().getStringExtra("name",some default value); 
} 
+0

我用於你告訴我:private void handleGetReviews(){ \t \t意圖我=新的意圖(RestaurantsActivity.this,ReviewCriteria.class); \t \t i.putExtra(「位置」 ,location.getText()。toString()); \t \t startActivity(i); \t}和Bundle extras = getIntent()。getExtras(); String location = extras.getString(「location」);但導致強制關閉 – xeon

+0

LogCat調試環境中的相應錯誤消息是什麼? – Vion

+0

好幾天後,沒有嘗試它,我發現這個問題,並在XML文件。你的方法完美運作。非常感謝:D – xeon

2

儘量放在包的數據以及與此捆綁

Intent intent = new Intent(this, YourSecondActivity.class); 
intent.putExtra(... HERE YOUR BUNDLE WITH DATA ...); 
startActivity(intent); 

希望開始活動,它幫助你!

相關問題