Я хочу получить три variables
с другого class
в моих проектах java (android) main activity
. Поэтому я положил их в bundle
и получил его в Intent
. Переменные из Object
я создал в третьем классе, который заключается в следующем:Получить дополнительные функции (переменные) из намерения
public class Transaction {
private float value;
private int transaction_Date;
private String description;
public Transaction(float value, int transaction_Date, String description){
super();
this.value = value;
this.transaction_Date = transaction_Date;
this.description = description;
}
public float getValue(){
return value;
}
public int getTransaction_Date(){
return transaction_Date;
}
public String getDescription(){
return description;
}
public void setValue(float value) {
this.value = value;
}
public void setTransaction_Date(int transaction_Date) {
this.transaction_Date = transaction_Date;
}
public void setDescription(String description) {
this.description = description;
}
Вот моя попытка получить данные в указанный Intent
:
public class AddMoneyTransaction extends AppCompatActivity implements View.OnClickListener {
Button addDepositButton;
EditText depositInput, depositInputDate, depositInputNote;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_money_transaction);
//setup the Button and EditText
addDepositButton = (Button)findViewById(R.id.addDepositButton);
depositInput = (EditText) findViewById(R.id.depositInput);
depositInputDate = (EditText) findViewById(R.id.depositInputDate);
depositInputNote = (EditText) findViewById(R.id.depositInputNote);
//get the onClickListener
addDepositButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent depositIntent = new Intent(AddMoneyTransaction.this, AssetsOverview.class);
Transaction deposit = new Transaction(100, 16, "random comment");
deposit.setValue(Float.parseFloat(depositInput.getText().toString()));
deposit.setTransaction_Date(Integer.parseInt(depositInputDate.getText().toString()));
deposit.setDescription(depositInputNote.getText().toString());
Bundle depositBundle = new Bundle();
depositBundle.putFloat("value", Float.parseFloat(depositInput.getText().toString()));
depositBundle.putInt("date", Integer.parseInt(depositInputDate.getText().toString()));
depositBundle.putString("description", depositInputNote.getText().toString());
depositIntent.putExtras(depositBundle);
startActivity(depositIntent);
}
И это (конечно) не работает:
//populated transaction list
protected void populateTransactionList() {
Intent depositIntent = getIntent();
Transaction deposit = (Transaction) getIntent().getExtras();
}
Так как я в состоянии получить свои три переменные из моей Intent
?
Итак, мое приложение перестало рушиться. Теперь, как я могу получить доступ к данным в комплекте после того, как получил это в своей основной деятельности?(Мне нужна переменная для работы (отображение данных в основном задании)) –
Я упомянул об этом в приведенном выше ответе о том, как получать данные всего вашего пользовательского объекта в другом действии. –
Добавлена переменная различного текстового вида, которую вы хотите передать. Дайте мне знать, если я что-то упустил? –