2016-12-25 27 views
-3

我用魔杖創建了一個Android應用程序,該應用程序有多個活動,我傳入數據然後發送給MainActivity。我希望通過創建意圖來傳遞數據然後使用savedInstanceState存儲它。 如果我通過了InformationA,但是當我發送數據InfomationB與另一個意圖存儲的信息A被刪除時,它會很好地工作。 我也嘗試使用共享首選項。如何通過活動發送數據並保存,以備後用?

我該如何解決這個問題,爲什麼是InformationA擦除?

public class MainActivity extends AppCompatActivity { 

String order = "Order \n"; 
String Order1; 
String Order2; 

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


    TextView warzywa = (TextView) findViewById(R.id.warzywa); 
    warzywa.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick (View View){ 
      Intent idź_w = new Intent(MainActivity.this, vegetables_activity.class); 
      startActivity(idź_w); 
     } 
    }); 
    TextView owoced = (TextView) findViewById(owoce); 
    owoced.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick (View View){ 
      Intent idź_w = new Intent(MainActivity.this, Owoce.class); 
      startActivity(idź_w); 
     } 
    }); 

    Button wysyłka = (Button) findViewById(R.id.zamówienie_całkowite); 

    wysyłka.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick (View View){ 
       if (Order1==null && getIntent().getExtras().getString("WhatO")!=null) 
       Order1 = getIntent().getExtras().getString("WhatO"); 
       if (Order2==null && getIntent().getExtras().getString("WhatV")!=null) 
       Order2 = getIntent().getExtras().getString("WhatV");  
       order = order + Order1 + Order2; 
       Toast.makeText(MainActivity.this, "" + order , Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 
public void onSaveInstanceState(Bundle savedInstanceState) { 
    savedInstanceState.putString("Order1", Order1); 
    savedInstanceState.putString("Order2", Order2); 
    super.onSaveInstanceState(savedInstanceState); 
} 
public void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 
    Order1 = savedInstanceState.getString("Order1"); 
    Order2 = savedInstanceState.getString("Order2"); 
}} 

1子活動

public class vegetables_activity extends AppCompatActivity { 
String Order = ""; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_warzywa); 
    final ArrayList<Produkt> vegetables = new ArrayList<>(); 
    vegetables.add(new Produkt("tomatoe", 0)); 
    vegetables.add(new Produkt("onion", 0)); 
    vegetables.add(new Produkt("potatoe", 0)); 

    New_Adapter adapter = new New_Adapter(this, vegetables); 

    ListView listView = (ListView) findViewById(R.id.activity_vegetables); 
    listView.setAdapter(adapter); 

    Button order = (Button) findViewById(R.id.zamówienie_warzywne); 
    order.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View View) { 
      for (int z = 0; z < vegetables.size(); z++) { 
       if (vegetables.get(z).getMquantity() != 0) { 
        Order = "" + Order + vegetables.get(z).getmNazwa_Warzywa() + " razy " + vegetables.get(z).getMquantity() + "\n"; 
       } 
      } 
      Context context = getApplicationContext(); 
      Toast.makeText(context, "OK", Toast.LENGTH_SHORT).show(); 
      Intent intent = new Intent(vegetables_activity.this, MainActivity.class); 
      intent.putExtra("WhatV", Order); 
      startActivity(intent); 
     } 
    }); 
}} 

2子活動

public class Owoce extends AppCompatActivity { 
String Order_Owoce = ""; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_warzywa); 
    final ArrayList<Produkt> vegetables = new ArrayList<>(); 
    vegetables.add(new Produkt("apple", 0)); 
    vegetables.add(new Produkt("banana", 0)); 
    vegetables.add(new Produkt("peach", 0)); 

    New_Adapter adapter = new New_Adapter(this, vegetables); 

    ListView listView = (ListView) findViewById(R.id.activity_vegetables); 
    listView.setAdapter(adapter); 

    Button order = (Button) findViewById(R.id.zamówienie_warzywne); 
    order.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View View) { 
      for (int z = 0; z < vegetables.size(); z++) { 
       if (vegetables.get(z).getMquantity() != 0) { 
        Order_Owoce = "" + Order_Owoce + vegetables.get(z).getmNazwa_Warzywa() + " razy " + vegetables.get(z).getMquantity() + "\n"; 
       } 
      } 
      Context context = getApplicationContext(); 
      Toast.makeText(context, "OK", Toast.LENGTH_SHORT).show(); 
      Intent intent = new Intent(Owoce.this, MainActivity.class); 
      intent.putExtra("WhatO", Order_Owoce); 
      startActivity(intent); 
      } 
    }); 
} 
} 
+1

顯示相關代碼。 –

+0

您是否將數據存儲到'''Bundle'''中?使用相同的密鑰?這會覆蓋原始數據。請發佈代碼以進一步瞭解。 – AshMenhennett

+0

添加了相關的代碼 – Maciek

回答

0

我想你可以嘗試的數組。將每條相關信息放在數組中的一個特定位置(應該在具有意圖的活動之間轉移),並將該信息的索引與意圖一起發送,以便接收活動知道在哪裏訪問它。

0

當您聲明您的意圖時,您可以將活動中的參數傳遞給另一個活動。

見例如:

Intent intent = new Intent(this, AnotherActivity.class); 
intent.putExtra("argKey", argValue); 

但要注意的是,捆綁只能存儲多達1MB,如果我沒有記錯的話,那麼當你需要通過例如一個巨大的數組,喜歡嘗試不同的方法SharedPreferences或將您希望保留的數據保存到DataManager單例類中,並在需要時進行檢索。

+0

我試過這樣做,但是在我看來,當我傳遞2次意圖時,我用savedInstanceState存儲的信息似乎消失了 – Maciek

相關問題