Home > Back-end >  Passing info into a new activity from main activity with android studio using java
Passing info into a new activity from main activity with android studio using java

Time:03-09

I am a beginner in Android studio, but I have worked with java previously. This is also my first time working with MongoDB Realm and it has been an uphill battle for me.I am working on an app that users choose a radio button, it then checks the database for the category of a charity(in main activity), it needs to submit that information to the next activity, and then prints the list of charities to the next activity(scrolling vertically). I am wondering how to best achieve it though. Any advice or recommendations would be greatly appreciated. I guess I am just not sure where to go from here. I also have a file with the object model from the database and a ToString method.

Here is what I have for mainactivity with the radio button & submitbtn:

        submitbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (medicalRadio.isChecked()) {
                RealmResults<NationalCharity> medicalcharity = uiThreadRealm.where(NationalCharity.class).equalTo("Category","Medical").findAll();
            }
            else if (envirRadio.isChecked()) {
                RealmResults<NationalCharity> envircharity = uiThreadRealm.where(NationalCharity.class).equalTo("Category","Environmental_Animal").findAll();
            }
            else if (hserviceRadio.isChecked()) {
                RealmResults<NationalCharity> hservicecharity = uiThreadRealm.where(NationalCharity.class).equalTo("Category","Human_Services").findAll();
            }
            else if (educationRadio.isChecked()) {
                RealmResults<NationalCharity> educationcharity = uiThreadRealm.where(NationalCharity.class).equalTo("Category","Education").findAll();
            }
            else if (publicaRadio.isChecked()) {
                RealmResults<NationalCharity> publicacharity = uiThreadRealm.where(NationalCharity.class).equalTo("Category","Public_Affairs").findAll();
            }
            else if (cultureRadio.isChecked()) {
                RealmResults<NationalCharity> cultureharity = uiThreadRealm.where(NationalCharity.class).equalTo("Category","Culture_Arts_Humanities").findAll();
            }
            else if (domesticvRadio.isChecked()) {
                RealmResults<NationalCharity> domesticvcharity = uiThreadRealm.where(NationalCharity.class).equalTo("Category","Domestic_Violence").findAll();
            }
            else if (hrightsRadio.isChecked()) {
                RealmResults<NationalCharity> hrightscharity = uiThreadRealm.where(NationalCharity.class).equalTo("Category","Human_Rights").findAll();
            }
            else if (homelessRadio.isChecked()) {
                RealmResults<NationalCharity> homelesscharity = uiThreadRealm.where(NationalCharity.class).equalTo("Category","Homelessness").findAll();

            } else if (religionRadio.isChecked()) {
                RealmResults<NationalCharity> religiouscharity = uiThreadRealm.where(NationalCharity.class).equalTo("Category","Religious").findAll();

            } else if (youthRadio.isChecked()) {
                RealmResults<NationalCharity> youthcharity = uiThreadRealm.where(NationalCharity.class).equalTo("Category","Youth").findAll();
            }



        }
        Intent intent = new Intent(MainActivity.this, ResultsActivity.class);
    });

Here's my To String method:

 public String toString() {
    results = "Charity name: "   Charity_name   " "   "Category: "   Category   "\n"  
            "Address: "   Address   " "   "City: "   City   " "   "State: "   State   " "  
            "Zip code: "   Zipcode   "\n"   "Mission Statement: "   Mission_statement   "\n"  
            "Web Address: "   Web_address;
    return results;
}

CodePudding user response:

One way to pass information to a new Activity is by adding Extra data to the Intent.

Intent intent = new Intent(MainActivity.this, ResultsActivity.class);
intent.putExtra("KEY_TO_DATA", result);
startActivity(intent);

Retrieve the data in the new Activity:

...inside onCreate...
if (savedInstanceState != null) {
    } else {
        Intent intent = getIntent();
        if (intent != null) {
            resultString = intent.getStringExtra("KEY_TO_DATA")
    }
}

Where KEY_TO_DATA can be anything you want. You can also pass integers and other data types to the Activity. Just make sure you retrieve it with the appropriate get???Extra. ie. intent.getIntExtra("KEY_TO_DATA")

There are other ways to pass data as well like using a ViewModel but that is a more involved answer.

  • Related