1. Are you ready for the Galaxy S20? Here is everything we know so far!

Apps data between two class

Discussion in 'Android Development' started by gsources75, Feb 21, 2011.

  1. gsources75

    gsources75 Lurker
    Thread Starter

    Hi, I know to load data from WebService o load other resource i need to creare a separate Thread. Ok i tryed and i't ok but all example show Thread inside the class but i'd like create a system of message between class, a second class load and parse data adn after sen data to firt class.

    Sorry for mybad english, there is the code


    package com.example;

    import android.app.Activity;
    import android.os.Bundle;

    Code (Text):
    1. public class MainClass extends Activity {
    2.     private UtilClass u_load;
    3.  
    4.     /** Called when the activity is first created. */
    5.     @Override
    6.     public void onCreate(Bundle savedInstanceState) {
    7.         super.onCreate(savedInstanceState);
    8.         setContentView(R.layout.main);
    9.        
    10.         u_load= new UtilClass();
    11.         u_load.loadData("http://www.mysite.com/data.xml");
    12.        
    13.        
    14.     }
    15. }

    Code (Text):
    1. package com.example;
    2.  
    3. public class UtilClass {
    4.     public UtilClass() {
    5.         // TODO Auto-generated constructor stub
    6.     }
    7.    
    8.     void loadData(String path){
    9.        
    10.        
    11.         Thread t = new Thread() {
    12.             public void run() {
    13.                 mResults = doSomethingExpensive();
    14.                 mHandler.post(mUpdateResults);
    15.             }
    16.         };
    17.         t.start();
    18.     }
    19.  
    20. }
    21.  
    I would have the task of loading data is given to the second class and after send data to MainClass. I don't want create UtilClass as Activity
     

    Advertisement

  2. JamTheMan

    JamTheMan Well-Known Member

    Well... You've already got it almost all the way there. The easiest understandable way to do it, is to give your UtilClass a reference to the caller in either its constructor like this:

    Code (Text):
    1. public UtilClass(MainClass main){
    2. ...
    3. }
    or in the method you call to do the heavy lifting like this:

    Code (Text):
    1. public void loadData(MainClass main){
    2. ...
    3. }
    Then you can define some public method in the MainClass which your UtilClass can call (in my example by calling main.somePublicMethod() ) whenever it is done crunching data.
     
  3. JamTheMan

    JamTheMan Well-Known Member

Loading...

Share This Page

Loading...