-2

My question is how do I pass variables between classes which both the classes extends Activity, yes I have 2 main classes

for example, I have a object class that has all the setters and getters which is then put in an ArrayList<object>, but when I set the variables in classA extends Activity and I want to pass the variables that I set for object class in classA so that I can get them in classB extends Activity

I tired just doing this in classB

ArrayList<object> objArrayList;

objArrayList.get(pos).getName;

But all I come back with is a null pointer.

I also did a Log.d("test", "name: " + objArrayList.get(pos).getName);

in classA it works fine but when I do this in classB, I get a nullpointer

Maroun
  • 94,125
  • 30
  • 188
  • 241
mug li
  • 3
  • 2
  • You can use SharedPreferences to pass data between activities see this link "http://stackoverflow.com/questions/23024831/android-shared-preferences-example" – Hanuman Feb 04 '15 at 07:33
  • Try using static to that variable, sharedpreferences or bundle with string array. – chiru Feb 04 '15 at 07:36
  • thx @Allu will check it out now – mug li Feb 04 '15 at 07:37
  • @Allu how do I use it as I am fetching the data from a JSON website to an ArrayList, so your saying use SharedPreferences instead of ArrayList? sorry I am not familiar with SharedPreferences is there any other options that which I can use ArrayList instead, cause if I switch to SharedPreferences I have to change everything – mug li Feb 04 '15 at 07:50
  • @chiru I have tried making the variables static and putting the data in a String array but still i get a nullpointer – mug li Feb 04 '15 at 07:51
  • @mugli the ArrayList is working in Activity A,then you can store ArrayList in SharedPreferences, and retrieve the array-list in Activity B ,and you can use as a array-list as u want here is the simple code to store arraylist. Retrieving Set set = myScores.getStringSet("key", null); //Set the values convert arralist into set Set set = new HashSet(); set.addAll(arraylist); scoreEditor.putStringSet("key", set); scoreEditor.commit(); – Hanuman Feb 04 '15 at 08:42
  • check this out... http://stackoverflow.com/questions/32267826/how-can-i-access-to-an-arraylist-from-another-class/32267999?noredirect=1#comment52415665_32267999 – Rohit Jagtap Aug 28 '15 at 12:05

3 Answers3

0

In order to pass variables between Activities, use your intent, with which you start your second activity, and the putExtra method:

Intent intent = new Intent(this, classB.class);
String name = objArrayList.get(pos).getName;
intent.putExtra("name",name);

In your classB you can get this parameter via the getIntent() method:

Bundle extras = getIntent().getExtras();
if(extras != null) {
   String name = extras.getString("name");
   ...
}
Simon
  • 454
  • 7
  • 18
0
    In first class,you need to start second activity 

      Intent i = new Intent(getActivity(), classB.class);
                i.putExtra("Value1", name);  //name is string value which is need to passed in second activity
                startActivity(i);

        In classB...
        Bundle extras = getIntent().getExtras();
            String value = extras.getString("Value1");  
//here as value we get string passed from first activity
shweta c
  • 107
  • 2
  • 12
  • While this may answer the OP's question, a few words of explanation would make this even more understandable to current and future readers. – Thom Aug 28 '15 at 12:52
0

Try this out...

In your first activity...

            Intent i = new Intent(this, SecondActivity.class);
            i.putExtra("My_ARRAY_LIST", arrayList);
            startActivity(i);

You can put whatever you want to sent String, int, boolean, ArraList etc. ( Object has to be serializable/parceable ) and in Second activity you should extract it from intent as,

Intent i = getIntent();
ArrayList<String> arraList = (ArrayList<String>) i.getSerializableExtra("MY_ARRAY_LIST");
Rohit Jagtap
  • 1,660
  • 1
  • 14
  • 12