14

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
MarcoS
  • 17,323
  • 24
  • 96
  • 174

7 Answers7

18

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);    
            }
        });
    }
}
Community
  • 1
  • 1
Houcine
  • 24,001
  • 13
  • 56
  • 83
  • sorry, which problem did you fix? – MarcoS May 17 '11 at 13:25
  • 1
    @Marcos : as i understand , you want to access to your activity from your class Points, which means , you want to modify and update your TextViews or your ImageViews etc , i 've edited your Code ,but Admins doesn't accept the edit, i will post it now in my Answer , wait a second :) – Houcine May 17 '11 at 14:02
  • Why would you edit it and not post an answer? wtf XD Fantastic answer though, thank you. – Sipty Mar 27 '15 at 14:07
  • 2
    @Sipty : I've edited his question for him to understand , because i didn't want to copy paste all the code in the answer, and Then i've added the edited part in my answer for others :D – Houcine Mar 27 '15 at 17:19
  • I'm trying to apply this to the app I created as part of following the Firebase "Getting Started" guide. I can receive messages and I see them in the Log. For the Listener when the app is backgrounded I even see them in the Notifications. But now I'd like to update a textView with the received message. Problem is the Firebase Message Service is apparently instantiated by magic, I don't have an opportunity to pass it my MainActivity context? – The Tahaan Oct 17 '16 at 21:43
4
  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
            }
        }
    }
    
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
1

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.

Rejinderi
  • 11,694
  • 2
  • 31
  • 40
1

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.

Haphazard
  • 10,900
  • 6
  • 43
  • 55
1

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;
  }
}
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
Ben Williams
  • 6,027
  • 2
  • 30
  • 54
0

i was in same trouble.. i found the simple way..

  1. make a static variable and function ...
  2. call from other class..

TestActivity.java

public class TestActivity extends Activity {

   static EditText edit_text1;

   public void onCreate(Bundle savedInstanceState) 
   {   
       .....

       edit_text1 = (EditText) findViewById(R.id.edit_text1);  

       .....
   }

   public static void setMSG(String str)
   {
       edit_text1.setText(str);
   }
}

Test2.java

TestActivity.setMSG("this is text");
nam
  • 25
  • 2
0

Could work using an interface

file MyActivity.java:

public class MyActivity extends Activity implements Points.MyListener {

    TextView myView;
    ... onCreate(...){
        myView = (TextView)findViewById(R.id.myView);
        Points myPoints  = new Points();

        //pass in MyActivity's instance of the listener
        myPoints.addListener(this);
    }

    @Override
    public void updateTextView(String message){
        myView.setMessage(message);
    }
}

file Points.java:

public class Points {

    public Points(){

    }

    public interface MyListener{
        void updateTextView(String message);
    }
    MyListener myListener;

    public void addListener(MyListener listener){
        myListener = listener;
    }

    public void updatePoints(){
        //do some operations in calculatePoints()
        String points = calculatePoints();
        //update views using MyActivity's implementation of updateTextView()
        myListener.updateTextView(points);
    }

}

Doing it this way, events can be fired / messages sent, for lack of better terms, from the external class to update the Activity UI. This might be overkill if all sb need is to call a method in the Points class that returns something

Ali
  • 2,702
  • 3
  • 32
  • 54
allo
  • 11
  • 3