0

I got a CameraView class (with an onPreviewFrame) which on each incoming frame calculates a value of 1 or 0 depends on the frame brightness, I want that on each incoming frame the calculated value will be add to an ArrayList. The problem begins when I need to read the data that is in the ArrayList from my MainActivity class.

How do I create a list that different class can get to?

I know it's relatively simple but i'm new to java/android and trying to wrap my head around how it all works.

Voly
  • 145
  • 4
  • 16

3 Answers3

0

Here is an example of what I recently used when I needed to send a HashMap from one activity to another:

Intent i = new Intent(getApplicationContext(), BillPage.class);
i.putExtra("workPriceMap", (HashMap<String, Float>)workPriceMap);
startActivity(i);

And then in my other class I used this:

private Map<String, Float> workPriceMap;
workPriceMap=(HashMap<String,Float>intent.getSerializableExtra("workPriceMap");
Trey50Daniel
  • 179
  • 3
  • 14
0

You can create a singleton class, like below:

public class Singleton {
    
    private static Singleton instance;
    
    private List<String> frameList;
    
    private Singleton() {
        this.frameList = new ArrayList<String>();
    }
    
    public static Singleton getInstance() {
        if(instance == null)
            instance = new Singleton();
            
        return instance;
    }

    public List<String> getList() {
        return this.frameList;
    }
    
    public void setList(List<String> frameList) {
        this.frameList = frameList;
    }
    
}

You can then access it from any class by:

Singleton.getInstance().getList().add("x");
Ryan M
  • 18,333
  • 31
  • 67
  • 74
ihsan
  • 576
  • 2
  • 5
  • 18
0

You could use a singleton method, this way you can have the same reference everywhere.

public  class YourClass {

    private ArrayList<String> yourList;
    private  YourClass instance;

    private YourClass(){
        yourList = new ArrayList<>();

    }
    public static YourClass getInstance(){
        if(instance == null){
            instance = new YourClass(); 
        }
        return instance;
    }

    public ArrayList<String> getYourList(){
        return this.yourList;
    }

} 

public class OtherClass{
    public static void main(String [] args){
        ArrayList<String> yourList  =  YourClass.getInstance().getYourList();
    }
}
Arturo Mejia
  • 1,862
  • 1
  • 17
  • 25