2011-06-29 6 views
0

私はActivityクラスとRestClientクラスを持っています。 JavascriptまたはActionScriptの操作RestClientにイベントリスナーを簡単に定義して、何かが発生したときにメインクラスがいくつかの関数をトリガーできるようにします。Android/JavaとaddEventListeners

のFlash例:

RestClient c = new RestCient() 
c.addEventListener(Event.DATA, dataHandler); 

function dataHandler(e:Event) 
{ 
... do something 
} 

アンドロイドでそれを行うための適切な方法は何ですか?主なクラスはアクティビティです。

アンドロイド例:

public class RestClient extends MyConnector 
{ 
    public RestClient() 
    { 
     ... 
    } 

    protected var onConnect() 
    { 
     dispatchEvent CONNECT ??? 
    } 

    protected var onClose() 
    { 
     dispatchEvent CLOSE ??? 
    } 

    ... 
} 

public class Main extends Activity 
{ 
    RestClient client; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     client = new RestClient(); 
     client.addEventListener(CONNECT, connectHandler); ????? 
    } 

    protected void connectHandler(Event e) 
    { 
     Log.i("Yeah!", "Connected!"); 
    } 
} 

感謝。

答えて

1

An AsyncTaskこれは、ActionScriptからのそのようなイベント処理に慣れている可能性があります。 AsyncTaskをサブクラス化し、メインアクティビティで以下のように定義することができます。

MyRestTask restTask = new MyRestTask<URL, Integer, Long>() { 
protected Long doInBackground(URL... urls) { 
    // instantiate an DefaultHTTPClient and call methods on it here 
} 

protected void onProgressUpdate(Integer... progress) { 
    // retrieve progress updates here 
} 

protected void onPostExecute(Long result) { 
    // process the response here 
} 
} 
+0

ha ... true my friend ...あなたは絶対に正しいです。私が持っている問題は、RestTaskが既にXから拡張されているため、サブクラス化できないということです。 – xpepermint

関連する問題