如何创建在两个不同 类 中工作的相同 HashMap,而不必在 Android Studio 中创建另一个 HashMap?

How to create the same HashMap that works in two different classes, without having to create another HashMap in Android Studio?

在第一个class中我创建了一个HashMap: HashMap test = new HashMap<>(); 然后将一个名为 name 的变量发送到数据库。代码:

HashMap<String, String> test = new HashMap<>();
test.put("name", name);

在Java中的第二个class中,我想发送一个叫surname的变量,在这个class中,但是必须在同一个HashMap中, 因为我想把它发送到同一个数据库。 请问如何在 Android Studio 中执行此操作?因为无法应用多重继承,因为 class Studio 中的 classes 已经扩展了 AppCompatActivity。

如果你只写:

test.put("surname", surname");

Android 不会理解名为“test”的对象,因为它没有在第一个 class.

中扩展 HashMap

如果您声明:

HashMap<String String> test = new HashMap<>();
test.put("surname", surname);

它将发送到另一个数据库,但我希望它在第一个 class 中发送到同一个数据库。

有人可以帮忙吗?

你可以创建常量class

class Constants {
 public static final HashMap<String, String> test = new HashMap<String,String>();
}

In other classes you can use the test HashMap

class Worker1 {
 public hashMapFunc() {
  Constants.test.put('this', 'this' );
 }
}

class Worker2 {
 public hashMapFunc() {
  Constants.test.put('that', 'that' );
 }
}

等等