How to access Activity UI from my class?

29,280

Solution 1

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

Solution 2

  1. Your Points can't be a private class without being an inner class. So your code doesn't even compile...
  2. Pass the view as parameter to the constructor of your Points class:

    public class MyActivity extends Activity {
        TextView myView = (TextView)findViewById(R.id.myView);
        Points myPoints new Points(myView);
    
        private class Points {
            public Points(TextView view) {
                // todo
            }
        }
    }
    

Solution 3

You should do everything and pass back the value to the activity to handle UI instead of doing any UI related stuff in the point stuff.

Solution 4

You can pass the main Activity's context (using Points(getApplicationContext());) to the class as a constructor parameter. You could also pass the specific UI elements you want to manipulate.

A better way to do it, however, may be to have Points not know about the Activity. Have your Activity call Points methods and take the necessary actions based on the method output.

Solution 5

You could just pass the view to your class.

Points myPoints = new Points(myView);

private class Points
{
  private TextView mTextView;

  Points(TextView textView)
  {
     this.mTextView = textView;
  }
}
Share:
29,280
MarcoS
Author by

MarcoS

I'm a sailor. I love roaming in the Mediterranean sea on any wood piece you can stick a mast and a sail on. In my spare time I do software engineering in Turin, as a full-stack developer. I start my coding ages ago with x86 assembly; then I go with C, bash, Perl, Java; Android; currently I do MEAN stack: MongoDB, Express.js, Angular.js and of course Node.js. Obviously on the shoulders of the GNU/Linux O.S..

Updated on December 06, 2020

Comments

  • MarcoS
    MarcoS over 3 years

    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?