2016-04-05 19 views
1

Bir bütçe uygulaması üzerinde çalışıyorum ve giderlerimi bir grafik oluşturacak olan sınıfa almada sorun yaşıyorum. Kullanıcı, para harcadıklarını gösteren bir onay kutusuyla birlikte bir masraf girebilecektir. Değeri MainActivity'ye gönderdiğimde, masrafı okur ve güncellenmiş bütçeyi gösterir, ancak sınıfımda bir grafik oluşturacak şekilde okumaya çalıştığımda, grafik boş görünür ve değerleri okumayacaktır. Eğer veri göndermek istediğinizdeDeğerler aynı anda iki farklı etkinliğe nasıl gönderilir

public class ExpensesActivity extends AppCompatActivity { 

    EditText editTextExpense; 
    Expenses expenses; 
    String selectedCheckBox; 
    CheckBox personalCheckBox; 
    CheckBox commuteCheckBox; 
    CheckBox billsCheckBox; 
    CheckBox funCheckBox; 
    CheckBox workCheckBox; 
    CheckBox foodCheckBox; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_expenses); 

     editTextExpense = (EditText)findViewById(R.id.moneySpent); 

     personalCheckBox = (CheckBox)findViewById(R.id.personalCheckBox); 
     commuteCheckBox = (CheckBox)findViewById(R.id.commuteCheckBox); 
     billsCheckBox = (CheckBox)findViewById(R.id.billsCheckBox); 
     funCheckBox = (CheckBox)findViewById(R.id.funCheckBox); 
     workCheckBox = (CheckBox)findViewById(R.id.workCheckBox); 
     foodCheckBox = (CheckBox)findViewById(R.id.foodCheckBox); 


     backToMainMenu(); 
    } 

    public void backToMainMenu() { 


     final Button expenseButton = (Button)findViewById(R.id.addExpenseSecondScreen); 
     expenseButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if (personalCheckBox.isChecked()) { 
        selectedCheckBox = personalCheckBox.getText().toString(); 
       } 
       if (commuteCheckBox.isChecked()) { 
        selectedCheckBox = commuteCheckBox.getText().toString(); 
       } 
       if (billsCheckBox.isChecked()) { 
        selectedCheckBox = billsCheckBox.getText().toString(); 
       } 
       if (funCheckBox.isChecked()) { 
        selectedCheckBox = funCheckBox.getText().toString(); 
       } 
       if (workCheckBox.isChecked()) { 
        selectedCheckBox = workCheckBox.getText().toString(); 
       } 
       if (foodCheckBox.isChecked()) { 
        selectedCheckBox = foodCheckBox.getText().toString(); 
       } 


       float expense = Float.parseFloat(editTextExpense.getText().toString()); 
       Intent intent = getIntent(); 
       intent.putExtra("expense", expense); 
       intent.putExtra("checkBoxText", selectedCheckBox); 
       setResult(RESULT_OK, intent); 

       finish(); 



      } 
     }); 
    } 


} 
+0

paylaşılan tercihi kullanın – Pavya

+0

Ayrıca, Yerel Yayını da kullanabilirsiniz. –

cevap

1

içinde kaydını ama olsun onActivityResult kullanılır startActivityForResult kullanarak bir Etkinlik başlatmanın sonucu ve sadece b e startActivityForResult adlı etkinliği çağırdı.

Faaliyetler yalnızca bir seferde aktif olduğundan (N önizlemesini hedeflemiyorsanız), sonuç verilerini diğer etkinliğin başlangıç ​​amacını kullanarak geçirmeniz gerekir.

0

Kullanım BroadcastReceiver hem etkinliğe BroadcastReceiver kayıt ve yangın BroadcastReceiver: İşte benim Gider Etkinlik (Ekran 2) 'dir.

public class MyReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show(); 
    } 
} 
registerReceiver(receiver, filter); 

BroadcastReceiver

oluşturmak için LocalBroadcastManager

örnek kodu kullanın ve ben burada ne bekliyoruz emin değilim onDestroy()

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    unregisterReceiver(receiver); 
} 
0

Ne istediğini tam olarak ne olduğunu bilmiyorum, sen gider ve

selectedCheckBox variables.Then için "statik" yargılanan kolayca herhangi bir yerde değerleri almak ya

paylaşılan tercihini kullanır.

İlgili konular