• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps data between two class

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:
public class MainClass extends Activity {
    private UtilClass u_load;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        u_load= new UtilClass();
        u_load.loadData("http://www.mysite.com/data.xml");
        
        
    }
}


Code:
package com.example;

public class UtilClass {
	public UtilClass() {
		// TODO Auto-generated constructor stub
	}
	
	void loadData(String path){
		
		
		Thread t = new Thread() {
            public void run() {
                mResults = doSomethingExpensive();
                mHandler.post(mUpdateResults);
            }
        };
        t.start();
	}

}

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
 
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:
public UtilClass(MainClass main){
...
}

or in the method you call to do the heavy lifting like this:

Code:
public void loadData(MainClass main){
...
}

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.
 
Back
Top Bottom