How to pass values from a Class to Activity - Android

前端 未结 6 1267
误落风尘
误落风尘 2021-02-09 08:23

I have a newbie question about Class/Task/Activity. I\'m coming from C so I don\'t know if it\'s a good approach to do what I need.

I\'ve created a class:



        
相关标签:
6条回答
  • 2021-02-09 08:36

    You can use Callback for this purpose.

    Define some interface like

    public interface MyCustomInterface(){
        public void sendData(String str);
    }
    

    Now let your Activity implement this interface.

    public class MyActivity implements MyCustomInterface {
    
    @Override
    public void sendData(String str){
    
    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
      @Override
      public void run() {
        recived_message.setText(str);
      }
    });
    }
    }
    

    Now in UDPServer.java, write the following code

    public class UDPServer {
    
    private MyCustomInterface interface;
    
    UDPServer(MyCustomInterface interface){
     this.interface = interface; 
    }
    
    }
    

    Now whenever you have some data available lets say a string, you can send it like this

    interface.sendData(str);
    
    0 讨论(0)
  • 2021-02-09 08:40

    You have an A activity and B one, when you finish actions on B activity side you need it to effect A side when you come back.

    1. Create an Instance Class and a method that u type u need, let's say;

      public interface SelectedBirthday {
      
          void onSelectedData(String date);
      }
      
    2. Now we are on B side, Create an instance of your Interface Class

      private SelectedBirthday mCallback;
      
    3. Override

      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
      
          try {
              mCallback = (SelectedBirthday) activity;
          } catch (ClassCastException e) {
              Log.d("MyDialog", "Activity doesn't implement the ISelectedData interface");
          }
      }
      
    4. Now upload the value you needed

      String userBirth = (day + " " + month + " " + year);
      
      mCallback.onSelectedData(userBirth);
      
    5. Ok let's go to A side

    Implement our Interface Class

    implements SelectedBirthday
    

    it will warn you for its method and you implemented it

         @Override
            public void onSelectedData(String date) {
                if (!date.equals("")) {
                    txt_poup_age.setText(date);
    //now you are free to do what you want with the value you received automaticaly
                }
            }
    
    0 讨论(0)
  • 2021-02-09 08:49

    Maybe you could use a Handler, load it with some data, and then read those data from your activity. Check more infos here about handlers

    You'd just pass an handler from your activity to your class, use handler.sendMessage("") inside your run method, and analyse what you receive inside your activity.

    0 讨论(0)
  • 2021-02-09 08:55

    In android 4 option to do this

    • In android you can send data through Intent or Intent followed by Bundle.Like Intent i = new Intent(current_class.this, linked_class.class); i.putextra("Key", value);

    And get the value(suppose string value) in another class like:

     String value = getIntent.getExtra("String key which you used when send value");
    
    • option 2

      class A{

      public static String _utfValue = "";

      void sendValue(){ _utfValue = "some value"; } }

    And fetch this value in your java class like:

    String value = A._utfValue ;
    
    • You can use SharedPreference to save the value and get it from other class.
    • You can use a static method with some return value and fetch the method in your java class through class name.
    0 讨论(0)
  • 2021-02-09 08:57

    there are many ways you can achieve this following are some

    1. you can use interfaces as explained in one of the answers
    2. you can create a IntentServce instead of your class and use Result Receiver to communicate the data.
    3. you can use handlers and messages as well
    4. you can also create a service and use IBinders (Bound Service)

    you can Google out more about these methods and chose what suits you better

    0 讨论(0)
  • 2021-02-09 09:02

    In your case I would use activity as interface. The interface is stored as a static parameter inside Application class.

    0 讨论(0)
提交回复
热议问题