将变量数据传递给非活动类 [英] passing variable data to non-activity classes
问题描述
将变量数据从<活动> 活动>类传递到<活动>非活动类的最佳方法是什么?数据是在第一个 Acivity
的 onCreate
中创建的,我需要将该数据传递给第二个类而不使用Intent.任何代码示例都将不胜感激.
What is the best way to pass variable data from an Activty
to a non-Activty
class? The data is created within the onCreate
of the first Acivity
I need to pass that data to the second class without the use of Intent. Any code samples would be appreciated.
自从我开始使用它以来,我就打算像它那样发布我所拥有的东西.就我而言,我已经有一个 Constructor
,它是一个 ArrayList
,为此,我需要另一个相同类型的对象.所以去了:
Since I got it to work like I intended it to post what I have. In my case I already had a Constructor
that was an ArrayList
and for what I was doing I needed another one of the same type. So here goes:
ClassActivity
ClassActivity
public class Polygon extends Overlay {
ArrayList<GeoPoint> geoPoints;
public Polygon(ArrayList<GeoPoint> points){
geoPoints = points;
}
public class Hex{
public Hex(ArrayList<String> str) {
// TODO Auto-generated constructor stub
Log.i("string",str.toString());
}
}
我的主要活动有
Polygon main = new Polygon(null);
Polygon.Hex mMainClass = (main.new Hex(color));
请注意,我必须在
Polygon.Hex mMainClass = (main.new Hex(color));
推荐答案
如果您的类名是 MainClass
,则为此创建 Constructor
并将值作为参数传递就是一个例子.
If your Class Name is MainClass
then Create Constructor
for that and pass value as a parameter, below is a example for that.
MainClass.java
MainClass.java
public class MainClass {
public MainClass(int i, String str) {
// TODO Auto-generated constructor stub
}
}
将以下代码用于从MainActivity到MainClass的传递值,其中 i
是整数变量,而 str
是字符串变量
Use below code for Pass Value from MainActivity to MainClass, Here i
is integer variable and str
is a String variable
MainClass mMainClass = new MainClass(i, str);
这篇关于将变量数据传递给非活动类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!