简体   繁体   English

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

[英]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 我想从其他类访问在主类中创建的hashmap集值。我已经按照它的步骤进行了操作但是我只在子类中获得了null值。这是代码

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 使HashMap静态化

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; 第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. 我不知道任何关于andriod实现的事情。 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() 使用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
}
}

Its really simple to pass Hashmap as argument, You just need to initialize it in the Parent class's constructor. 将Hashmap作为参数传递非常简单,只需要在Parent类的构造函数中初始化它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM