2013-01-24 39 views
0

我從我的應用程序的以前的活動中獲取某些內容時遇到問題。我的情況是,以前的活動,被稱爲ListOfMeals,它有一個列表視圖(早餐,早上點心等)如果我點擊早餐並添加膳食,我會點擊列表的食物,如麪包,水果,蔬菜,等。之後,它應該添加到我的數據庫。但我到目前爲止是這樣的,請參考下面:ANDROID從以前的活動中獲取數據

case R.id.btFoodVegetableSave: 
     String mealname = selected; 

     String serving = calories.getText().toString(); 
     int i = Integer.parseInt(serving.replaceAll("[\\D]", "")); 

     String servng = String.valueOf(i); 

     SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy"); 
     String strDate = sdf.format(new Date()); 

     if ((mealname.isEmpty() || servng.isEmpty())){ 

      // call for custom toast 
      viewErrorToast(); 
     } 

     else { 

     boolean didItWork = true; 

     try{ 

      BreakFastLog entry = new BreakFastLog(Baguettes_Bagels.this); 
      entry.open(); 
      entry.createEntry(mealname, servng, strDate); 
      entry.close(); 

      } 
      catch(Exception e){ 
       didItWork = false; 
       viewErrorToast(); 
      }finally{ 
       if (didItWork){ 
        viewBMRSavedToast(); 
       } 

      } 
     } // end of if else statement 
     break; 

在我的例子中,它只保存到我的早餐。但是,當我選擇早上點心而不是早餐時,問題就會出現在這裏。如何獲得用戶從以前的活動中選擇的內容並將其保存到各自的餐點?

我知道這已經得到的東西與此有關:

BreakFastLog entry = new BreakFastLog(Baguettes_Bagels.this); 
     entry.open(); 
     entry.createEntry(mealname, servng, strDate); 
     entry.close(); 

我必須把是否else語句,但我想不出來實現這一些巧妙的方法。感謝您的幫助提前。

回答

3

當您啓動活動,你有可能是這樣的:

Intent intent = new Intent(...); 
startActivity(intent); 

您可以添加:

intent.putExtra(key, yourparameter); 

而且你的第二個活動開始時,你可以在OnCreate例如retreive它:

int value = getIntent().getExtras().getInt(key); 
相關問題