简体   繁体   中英

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?

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

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);

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
}
}

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"));
}
}

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.
}

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