Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Activity UI from my class?

I have an activity which creates an object instance of my class:

file MyActivity.java:
public class MyActivity extends Activity {
    TextView myView = (TextView)findViewById(R.id.myView);
    ...
    Points myPoints new Points();
    ...
}
--------------------------------------------------------------

file Points.java:
private class Points {
    ...
    HOW TO USE myView HERE ???
    ...
}
--------------------------------------------------------------

How do I use the UI objects in my class (which does not extend an Activity)? Should I pass some context to my Points class? How do I do, exactly?

like image 466
MarcoS Avatar asked May 17 '11 12:05

MarcoS


People also ask

How do I find my instance of activity?

onCreate(savedInstanceState); setContentView(R. layout. main); activity = this; // Create an instance of Camera mCamera = getCameraInstance(); // Create our Preview view and set it as the content of our activity. mPreview = new CameraPreview(this, mCamera); FrameLayout preview = (FrameLayout) findViewById(R.

What is onCreate Method in Android?

Android App Development for Beginners onCreate() is called when the when the activity is first created. onStart() is called when the activity is becoming visible to the user.

How do I find my activity class name?

Use this. getClass(). getSimpleName() to get the name of the Activity.


1 Answers

see you post, i've edited it , to fix the problem

hope it helps :=)

here is the Edit :

file MyActivity.java:
    public class MyActivity extends Activity {
    TextView myView ;
    protected void onCreate(android.os.Bundle savedInstanceState) {
        myView = (TextView)findViewById(R.id.myView);
            Points myPoints = new Points(this);
            myPoints.displayMsg("Hello World !!!");
    }  
    }

--------------------------------------------------------------

file Points.java:
private class Points {
    protected MyActivity context;
    //add a constructor with the Context of your activity
    public Points(MyActivity _context){
        context = _context;
    }

    public void displayMsg( final String msg){
        context.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                context.myView.setText(msg);    
            }
        });
    }
}
like image 125
Houcine Avatar answered Oct 06 '22 01:10

Houcine