Karthik
Karthik

Reputation: 5033

How to access hashmap values from another class?

I would like to access hashmap set of values created in main class from other class.I have followed the steps for it but I am only getting null value at the sub-class.Here is the code

public class SoapTester extends Activity {  
private static final String TAG = "Test";  
public HashMap<String, String> map = new HashMap<String, String>();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    map.put("year", "Apple");
    map.put("make", "Mango");
    map.put("model", "Grape");
    map.put("style", "Orange");
    map.put("series", "Peach");
}

public HashMap<String, String> getHashmap() {
    Log.v(TAG, "map2: E" + map);
    return map;
}

public void setHashmap(HashMap<String, String> map) {
    this.map = map;
    getHashmap();
    Log.v(TAG, "map1: E" + map);
}
}

//Sub Class

public class Tradein extends Activity {
private static final String TAG = "Test";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tradein);
    SoapTester ex = new SoapTester();
    HashMap<String, String> hm = ex.getHashmap();
    Log.v(TAG, "hm: " + hm);//Getting Null Value here
}
}

Have I missed anything?

Upvotes: 2

Views: 15120

Answers (5)

Suresh Sharma
Suresh Sharma

Reputation: 57

Its really simple to pass Hashmap as argument, You just need to initialize it in the Parent class's constructor.

Child Class:

HashMap<String, String> map = new HashMap<String, String>();;
    map.put("OS", "Android");
Parent parent= new Parent();
parent.hashtest(map);

Parent Class:

public class parent{
HashMap<String, String> map;
public Test() {
    map= new HashMap<>();
}

public void hashtest(HashMap<String, String> map){
    this.map=map;
    Log.v("I fount it here", map.get("OS"));
}
}

Upvotes: 0

Garry
Garry

Reputation: 4533

use this.getHashmap() instead of ex.getHashmap()

public class Tradein extends Activity {
private static final String TAG = "Test";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tradein);
    //SoapTester ex = new SoapTester();
    HashMap<String, String> hm = this.getHashmap();
    Log.v(TAG, "hm: " + hm);//Getting Null Value here
}
}

Upvotes: 1

DRCB
DRCB

Reputation: 2121

You can fill in the map directly in the class initialization:

public class SoapTester extends Activity {  
private static final String TAG = "Test";  
public static HashMap<String, String> map = new HashMap<String, String>() {
    {
        put("year", "Apple");
        put("make", "Mango");
        put("model", "Grape");
        put("style", "Orange");
        put("series", "Peach");
    }
};

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // No map initialization here
    }

// etc.
}

Upvotes: 0

jennifer
jennifer

Reputation: 8261

make the HashMap static

public static HashMap<String, String> map = new HashMap<String, String>(); 

In this way we can change values in any activity at will, regardless of the exact flow of control between the various activities.

Note that this trick can only be used if you don't care about the instantiation of more than one copy of the same activity (class) in the application, this is the easiest to implement

Step 2 : Android; Implementing global state; share data between Activities and across your application

Upvotes: 10

Vaandu
Vaandu

Reputation: 4945

Not an answer, just a try.

I don`t know anything about andriod implementation. But here is my try.

SoapTester ex = new SoapTester();
ex.onCreate(savedInstanceState);
HashMap<String, String> hm = ex.getHashmap();
Log.v(TAG, "hm: " + hm);

Upvotes: 1

Related Questions