简体   繁体   English

将数组对象推送到 Array 或 ArrayList 并从 Android 中的另一个类访问它

[英]Pushing an object of arrays to an Array or ArrayList and access it from another class in Android

I have an object of arrays and I am pushing it to an ArrayList like this:我有一个数组对象,我将它推送到这样的 ArrayList 中:

static ArrayList<String> jsonResponse = new ArrayList<>();

//inside the method
strResp = response.body().string(); //the response I get from the server
jsonResponse.clear();  //clear the array first
jsonResponse.add(strResp);

Now from the other class, I am accessing it like this but I can't get it to print because it says NULL:现在从另一个类,我像这样访问它,但我无法打印它,因为它说 NULL:

public class SecondClass extends ReactContextBaseJavaModule {

private FirstClass firstclass;

  public void getLocks(Callback callback) {
  System.out.println("Print object: " + firstclass.jsonResponse);
  }
}

First question, is ArrayList a proper way of achieving what I want in this case?第一个问题,在这种情况下,ArrayList 是实现我想要的正确方法吗?

Second question, why am I not able to print it in the other class?第二个问题,为什么我无法在其他班级中打印它?

I do not have any experience in Android/Java so I know this might sound like a dumb question to many of you but please try to understand!我在 Android/Java 方面没有任何经验,所以我知道这对你们中的许多人来说听起来可能是一个愚蠢的问题,但请尝试理解!

You are creating a FirstClass variable, but you are not initiating it.您正在创建 FirstClass 变量,但并未启动它。 FirstClass is an object which contains a jsonResponse list. FirstClass 是一个包含 jsonResponse 列表的对象。 If an object is not initiated then the list itself will always be null.如果未启动对象,则列表本身将始终为空。 You need to call the constructor of FirstClass like:您需要像这样调用 FirstClass 的构造函数:

firstclass = FirstClass(<add parameters here if any>);

Hope this helps :)希望这可以帮助 :)

you create private list of strings in the class and then try to access it from another class.您在类中创建字符串的私有列表,然后尝试从另一个类访问它。 In addition your object have not initialized另外你的对象还没有初始化

1) In the code every time before you insert a string, you clear the list. 1)在每次插入字符串之前的代码中,您清除列表。 so if you don't use more than one string just use String.因此,如果您不使用多个字符串,请使用 String。

2) Init your object and add 'public' before the static and it should work. 2)初始化您的对象并在静态之前添加“公共”,它应该可以工作。 ie: IE:

firstclass = FirstClass();

and

public static ArrayList<String> jsonResponse = new ArrayList<>();

In Java to access static elements, you don't need instance of the class(FirstClass).在 Java 中访问静态元素,您不需要类(FirstClass)的实例。 You simply do你只是做

FirstClass.jsonResponse(); FirstClass.jsonResponse();

The reason why you are getting null is be because you are trying to print the List object, which is not a String but a list of Strings.你得到 null 的原因是因为你试图打印 List 对象,它不是一个字符串,而是一个字符串列表。 So do a the following:因此,请执行以下操作:

if(!jsonResponse.isEmpty()){
    //print the first String in the List
    jsonResponse.get(0);
}

To answer your other question, LinkedList has more functionality, but List is faster.要回答您的另一个问题,LinkedList 具有更多功能,但 List 速度更快。 Check this out for more explanation . 查看更多解释

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

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