2

As we can send String type to another activity like this

public static final String EXTRA_MESSAGE = 
               "com.example.android.twoactivities.extra.MESSAGE";

what should be the code for this

private static final ArrayAdapter LIST_OF_CUSTOMERS = 

P.S.- I am writing this code in MainActivity and want to send Database in the form of ListView to another activity named saveScreen

1 Answers1

0

My first suggestion would be why cant the second activity simply query the database itself?

Other then that if you must I'd suggest getting the results from the ArrayAdapter into an ArrayList and using Bundle.putParcelableArrayList when calling the second activity.

See here or this for passing Bundles to Activities and reading their values back again, but essentially you would do something like this when calling the second activity:

Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("LIST_OF_CUSTOMERS", arrayListOfCustomers);
intent.putExtras(bundle);
startActivity(intent);

And inside the second Activity:

ArrayList<..> customers = getActivity().getIntent().getParcelableArrayListExtra<..>("LIST_OF_CUSTOMERS");
if (customers != null) {
    // do something with the data
}

The only thing to remember is that whatever type your list is of i.e. ArrayList<Customer>, Customer class needs to implement the Parcelable interface. See here or here for more.

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • 1
    Thanks @David for your resolution that was really helpful. Since I'm new to this, I still don't know a lot of things, so I'm trying to figure things out! – Suryansh Kushwaha Nov 20 '20 at 10:10
  • It would mean a lot to me if you would suggest me someplace nice from where I can learn Android Development using Java – Suryansh Kushwaha Nov 20 '20 at 10:12
  • @Suryansh Kushwaha glad I could help... uhm thats a hard one, I usually find that in order to learn a language or tooling you need to have an idea and ad you bring it to life you learn more and more. But if you like reading documentation, learning the concepts of course helps as you will know better the terminology when Googling for answers. See https://developer.android.com/guide the menu options really have a lot of the various concepts and nice explanations etc. That would be where I'd start – David Kroukamp Nov 20 '20 at 15:43