[英]Data accessing from one class to another class
Is there any way to call one class's ArrayList onto another class? 有什么办法可以将一个类的ArrayList调用到另一个类上? In class A ArrayList is there, I need to use that ArrayList in class B, Class A is having
AsyncTask
in that I have the Arraylist so I need that in side AsynTask's Arraylist in another class. 在A类的ArrayList中,我需要在B类中使用该ArrayList,A类具有
AsyncTask
,因为我具有Arraylist,所以我需要在另一个类的AsynTask的Arraylist中使用它。
In class A: 在A类中:
public Class A{
public class HomeTask extends AsyncTask<Void,Void,Bundle> {
onPreexecute(){}
doInbackground(){}
postexecute(){}
Arraylist joblist(); //------------ My data is here;
}
}
second class: 第二类:
Class B
{
// I need to use that arraylist here
}
Give an instance of class A to class B's constructor and store it in a field. 将类A的实例提供给类B的构造函数,并将其存储在字段中。 Then both A & B istances share the same job list.
然后,两个A和B位置共享同一作业列表。
Class B {
private A a;
public B(A _a) {
a=_a;
}
}
Then any method in class B can read the joblist by calling a.joblist(); 然后,类B中的任何方法都可以通过调用a.joblist()来读取作业列表。
declare your arraylist into the main class. 在主类中声明您的arraylist。
Public Class A{
ArrayList yourlist;
public class HomeTask extends AsyncTask<Void,Void,Bundle>
{
onPreexecute();{}
doInbackground();{}
postexecute();{}
Arraylist joblist()------------ My data is here;
}
}
second class:
Class B
{
I need to use that arraylist here
}
}
or you can create the object of HomeTask in main class and through this object used it like 或者您可以在主类中创建HomeTask的对象,并通过该对象使用它,例如
Public Class A{
HomeTask hometask;
public class HomeTask extends AsyncTask<Void,Void,Bundle>
{
onPreexecute();{}
doInbackground();{}
postexecute();{}
Arraylist joblist()------------ My data is here;
}
}
Class B
{
ArrayList al = hometask.joblist();
}
}
I am going to make some assumtions. 我要作一些假设。 I notice that you have an inner class HomeTask in ClassA and you are attempting to call a method on the inner class from some other ClassB .
我注意到您在ClassA中有一个内部类HomeTask ,并且您正在尝试从其他ClassB调用内部类上的方法。 If this is true than there must be an instance of the inner class HomeTask in ClassA unless HomeTask is a static inner class which currently it is not.
如果为真,则在ClassA中必须有内部类HomeTask的实例,除非HomeTask是静态内部类(当前不是)。 So...
所以...
If ClassA is: 如果ClassA为:
public Class A{
public class HomeTask extends AsyncTask<Void,Void,Bundle> {
onPreexecute(){}
doInbackground(){}
postexecute(){}
Arraylist joblist(); //------------ My data is here;
}
private HomeTask homeTask = new HomeTask();
public HomeTask getHomeTask(){
return this.homeTask;
}
}
And ClassB; 和ClassB;
public class ClassB{
private ClassA classA = new ClassA();
private HomeTask homeTask = classA.getHomeTask();
private ArrayList arrayList = homeTask.jobList();
}
The best way to do using the Observer.Please check the document in java web site.where onupdate you can right the code and also access from any where in application. 使用Observer的最佳方法。请检查java网站上的文档。在onupdate上,您可以对代码进行纠正,也可以从应用程序中的任何位置进行访问。 If you want more help then tell me i will post the code.
如果您需要更多帮助,请告诉我,我将发布代码。
Thank you. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.