简体   繁体   中英

Android Studio, Passing 1 ArrayList between 2 Activites

I am working on a simple Android application and have 2 activities and 1 arraylist declared in the mainactivity.java file.

I'm able to get the data from the second activity and add to arraylist.

However everytime it switches from the second activity to the main activity, my arraylist gets cleared.

I need suggestions on how I could potenially pass the array list between 2 activities.

As a reminder, I'm only passing cause it's the only way I can think off to pass retain arraylist data.

MainActivity.java

public class MainActivity extends AppCompatActivity {

//declaring and building array list
ArrayList<Items> expenses = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //obtaining the data from addExpenses.java
    String date = getIntent().getStringExtra(addExpenses.DATE2);
    String amount = getIntent().getStringExtra(addExpenses.AMOUNT2);
    String payee = getIntent().getStringExtra(addExpenses.PAYEE2);
    String category = getIntent().getStringExtra(addExpenses.CATEGORY2);
    String payment_method = getIntent().getStringExtra(addExpenses.PAYMENT_METHOD2);
    String more_description = getIntent().getStringExtra(addExpenses.MORE_DESCRIPTION2);

    //add the data from addExpenses.java into a position of the arraylist
    expenses.add(new Items(date, amount, payee, category, payment_method, more_description));

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            //add code here prehaps to pass arraylist to my second activity?

            //go to addExpenses activity
            Intent intent = new Intent(getApplicationContext(), addExpenses.class);
            //go to second activity
            //passing the intent inside
            startActivity(intent);
        }
    });
}

My second activtiy:

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

    //put code here to recieve information from MainActivity

}

public void addNewExpense() {
    Intent intent = new Intent(getApplicationContext(), MainActivity.class);

    //get the date inputted by user
    String date = datePlainText.getText().toString();
    //save the user selection to intent
    intent.putExtra(DATE2, date);

    //get the amount inputted by user
    String amount = amountPlainText.getText().toString();
    //save the user selection to intent
    intent.putExtra(AMOUNT2, amount);

    //get the payee inputted by user
    String payee = payeePlainText.getText().toString();
    //save the user selection to intent
    intent.putExtra(PAYEE2, payee);

    //get the category inputted by user
    String category = categorySpinner.getSelectedItem().toString();
    //save the user selection to intent
    intent.putExtra(CATEGORY2, category);

    //get the date inputted by user
    Integer checkedId = paymentMethodRadioGroup.getCheckedRadioButtonId();
    String paymentMethod = "";
    switch (checkedId) {
        case R.id.cashRadioButton:
            paymentMethod = "Cash";
            break;
        case R.id.creditRadioButton:
            paymentMethod = "Credit Card";
            break;
        case R.id.mobileRadioButton:
            paymentMethod = "Mobile App";
            break;
        default:
    }
    intent.putExtra(PAYMENT_METHOD2, paymentMethod);

    //get the more description inputted by user
    String moreDescription = descriptionPlainText.getText().toString();
    //save the user selection to intent
    intent.putExtra(MORE_DESCRIPTION2, moreDescription);

    //go to intent actvitiy
    startActivity(intent);


}

}

im not understand your code but im alittle bit understand your problem ( that u wanna pass arraylist between Activites) anyway allow me to give you another way

create

 class anything{
private static ArrayList<Items> GetExpenses = new  ArrayList<Items>();

public static ArrayList<Items> getExpenses() {
    return GetExpenses;
   }

public static void setgetExpenses(ArrayList<Items> getExpenses) {
    GetExpenses = getExpenses;
   }
}

set it from any activity

anything.setgetExpenses(expenses);

get it from any activity

expenses = anything.getExpenses();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM