简体   繁体   中英

Data accessing from one class to another class

Is there any way to call one class's ArrayList onto another class? 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.

In class 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. Then both A & B istances share the same job list.

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();

declare your arraylist into the main class.

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

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

If ClassA is:

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;

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. If you want more help then tell me i will post the code.

Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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