简体   繁体   中英

Pass data from View class to Activity class in Android

In my android application, I'm going to color a bird sketch. I need to color each part of the bird with various colors. I identify those parts with screen co-ordinates of this bitmap. Now what I need is, when user touch the area of a body part, then to open an another window which has color list. For that I need to pass data from my View class to new Activity class. How can I do that? Please give a help!

My View Class

class BirdColors extends View{

    private Paint paint;
    public Bitmap mBitmap,nBitmap;
    public Canvas canvas;
    private int x,y;
    private CreateColorList colorList;
    int val;

    public BirdColors(Context context) {
        super(context);
        // TODO Auto-generated constructor stub 
        paint=new Paint();
        paint.setAntiAlias(true);   
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);

        mBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.birdsketchfinal2).copy(Config.ARGB_8888, true);


        mBitmap= Bitmap.createScaledBitmap(mBitmap, 230, 230, true);
        //mBitmap=nBitmap;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);


        this.canvas=canvas;
        canvas.drawBitmap(mBitmap,0,0, null);
        //canvas.drawText("Shashika", 10, 50, paint);
        canvas.drawText("Screen Cordinates" +x+"x"+y, 10, 220, paint);


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
         switch(event.getAction()){

        case MotionEvent.ACTION_DOWN:

            x=(int)event.getX();
            y=(int)event.getY();

            if((x>5 && x<23)&&(y>30 && y<47)){

                    val=1;
                    //colorList=new CreateColorList(val);
                    //In here I need to pass the val 

            }
            invalidate();
         }
        return true;
    }

You can do this by creating interface like this way

public class BirdColors extends View{

   //.........your code ............

// create local object of BirdColorListener

   private BirdColorsListener local;

// create seter/geter methods

   public void setBirdColorListener(BirdColorsListener birdColorListenr){
   this.local = birdColorListenr;
   }
   public BirdColorsListener setBirdColorListener{
     return this.local;
   }


    @Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
     switch(event.getAction()){

    case MotionEvent.ACTION_DOWN:

        x=(int)event.getX();
        y=(int)event.getY();

        if((x>5 && x<23)&&(y>30 && y<47)){

                val=1;
                //colorList=new CreateColorList(val);
                //In here I need to pass the val 

             if(getBirdColorListener()!=null){
               getBirdColorListener().onBirdTouch(val);
             }


        }
        invalidate();
     }
    return true;
}


   //Add this things

 public interface BirdColorsListener{

   void onBirdTouch(int val);
 }

}

In your Activity;

public class BirdActivity extends Activity{

  BirdColors bc = new BirdColors();




  protected void onCreate(){

    bc.setBirdColorListener(new BirdColorsListener() {

            @Override
            public void onBirdTouch(int val) {
                // you will get "val"  from your view

            }
        });
  }

}

When you construct your own Class ,you can pass the reference of the object of the outer Activity, so when you hold the event in your own view,you can invoke the method by that reference and pass data to it.
Such as:

Class OuterActivity externd Activity{
 public void setVal(int color){
 }
...
...
 View yourView = new YourView(this);
}

so in your view:

in constructer:

YourView(Activity inarg){
 this.mOuterActivity =inarg;
}

...//and hold the event

this.mOuterActivity.setVal(0x0099ff);

I had the same problem but the other solutions (who are probably the best ones) didn't work for me so what i did is i made a global variable like this to pass data from my view back to my activity:

public static ArrayList<Coordinates> coordinates = new ArrayList<Coordinates>();

it's in a separate class called GlobalStatic so now i can call that variable like this everywhere :

GlobalStatic.coordinates....

and to get info into my view I just made a function in that view and call that one in my activity:

in view

public void setStatementId(int id) {
    statementId = id;
}

in activity

BrushView S_map = new BrushView(this);
        S_map.setStatmentId(statementId);

it's not a nice solution but it works for me.

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