如何从另一个类访问hashmap值?

时间:2012-01-04 09:25:59

标签: java android collections hashmap

我想从其他类访问主类中创建的值的hashmap集合。我已经按照它的步骤进行了操作但是我只在子类中获得空值。这是代码

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

//子类

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

我错过了什么吗?

5 个答案:

答案 0 :(得分:8)

使HashMap静态

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

通过这种方式,我们可以随意更改任何活动中的值,无论各种活动之间的确切控制流程如何。

请注意,只有在您不关心应用程序中同一活动(类)的多个副本的实例化时才能使用此技巧,这是最容易实现的

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

答案 1 :(得分:1)

不是答案,只是一试。

我对andriod的实施一无所知。但这是我的尝试。

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

答案 2 :(得分:1)

使用 this.getHashmap()代替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
}
}

答案 3 :(得分:0)

您可以直接在类初始化中填写地图:

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

答案 4 :(得分:0)

将Hashmap作为参数传递非常简单,只需要在Parent类的构造函数中初始化它。

儿童班:

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

父类:

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