Cael
Cael

Reputation: 556

Android: Passing data from Java file to an activity

I have a Java file that contains Java function that is called in an activity Java. However I don't know how to pass some data from the Java function file to the activity Java so that I could display it in the app.

Some coding in the Java function file (FaceView.java)

public class FaceView extends View {

private FaceListener mFaceListener;

interface FaceListener {
     void handleData(int[] cxArr, int[] cyArr);    
}

public void setFaceListener(FaceListener faceListener) {
    this.mFaceListener = faceListener;
}

private int cx;
private int cy;
private int[] cxArr;
private int[] cyArr;

private void drawFaceAnnotations(Canvas canvas, double scale) {
    for (int i = 0; i < mFaces.size(); ++i) {
        Face face = mFaces.valueAt(i);

        for (Landmark landmark : face.getLandmarks()) {
            cx = (int) (landmark.getPosition().x * scale);
            cy = (int) (landmark.getPosition().y * scale);
            canvas.drawCircle(cx, cy, 5, paint);
            cxArr[i] = cx;
            cyArr[i] = cy;
            mFaceListener.handleData(cxArr,cyArr);
        }
}
    }
}

Code in Activity java file (DisplayActivity.java)

 overlay.setFaceListener(new FaceView.FaceListener() {
 public void handleData(int[] cxArr, int[] cyArr) {

 System.out.println(cxArr[1]+" "+cyArr[1]);
     }
  });

Anyone have any idea how to pass data from FaceView.java to DisplayActivity.java? I tried intend/bundle but doesn't seems to be working. Thanks

Upvotes: 0

Views: 95

Answers (2)

Devrim
Devrim

Reputation: 15533

You need a callback from your view to your activity.

Add an interface to your View implementation and invoke it when you need:

public class FaceView extends View {

    private FaceListener mFaceListener;

    interface FaceListener {
        public void handleData(Integer[] cxArr, Integer[] cyArr);
    }

    public void setFaceListener(FaceListener faceListener) {
        this.mFaceListener = faceListener;
    }


    private void drawFaceAnnotations(Canvas canvas, double scale) {
        // your code here
        // notify listener

       List<Integer> cxList = new ArrayList<Integer>();
       List<Integer> cyList = new ArrayList<Integer>(); 

       for (int i = 0; i < mFaces.size(); ++i) {
           Face face = mFaces.valueAt(i);

           for (Landmark landmark : face.getLandmarks()) {
                cx = (int) (landmark.getPosition().x * scale);
                cy = (int) (landmark.getPosition().y * scale);
                canvas.drawCircle(cx, cy, 5, paint);

                cxList.add(cx);
                cyList.add(cy);

            }
        }

        Integer[] cxArr = cxList.toArray(new Integer[cxList.size()]);
        Integer[] cyArr = cyList.toArray(new Integer[cyList.size()]);

        mFaceListener.handleData(cxArr,cyArr);
    }
}

Set callback/interface at your activity:

public class DisplayActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // your code here
        FaceView overlay = (FaceView) findViewById(R.id.faceView);
        overlay.setFaceListener(new FaceView.FaceListener(){
             public void handleData(int[] cxArr, int[] cyArr) {
                 // TODO: handle faces array here
             } 
        });
    }
}

Upvotes: 1

Henry
Henry

Reputation: 17841

You could simply have a getter method in FaceView class. Since you have a reference to this class in the DisplayActivity, you could simply call this method to pass the data.

So from you comment, you need to pass cx and cy. I take it these are int coordinates. Since you can return only one value in java method and you have two, I am using Point. But you could use Array or ArrayList. Do something like the following.

FaceView.java

 private int cx;
 private int cy;

 public Point getCoordinates(){
       return new Point(cx, cy);
 }

DisplayActivity.java

 final Point point = overlay.getCoordinates();
 int cx = point.x;
 int cy = point.y;

This is not technically passing data from FaceView to DisplayActivity, but fetching data of FaceView from DispalyActivity. You can use this method if this is sufficient for you. I suggest this from your comment The reason for the data passing is that I will create a text view to display the data on the app.

On the other hand, if you want to notify DisplayActivity immediately about a change in FaceView then you need to set a listener for it via creating an iterface (See DevrimTuncer's answer).

Upvotes: 0

Related Questions