0

現在、10 サンプルごとなど、定期的に位置情報の更新を収集しようとしています。arraylist を使用してそれらを収集し、非同期タスクを使用してそれらをサーバーに渡したら、メイン UI スレッドで arraylist をクリアします。非同期タスクでは、メイン UI からの配列リストをロードしています。

問題は、別の変数であっても、非同期タスクで配列リストをクリアしていることです。アクティビティの同期を維持するにはどうすればよいですか。非同期タスクが完了するまでメイン アクティビティをスリープ状態にする必要がありますか。変数についてはわかりません。誰かがこれを行う方法を説明できますか?

MainMapActivity(X){
  locationupdate for every 1 min{
  arraylist a;//this collects all location updates 10 samples each time
  call asynctask b;
  clear a;
}
asynctask b{
  arraylist c = getall from  a;
  db= insert(c);//save a into database;
}

メイン UI で a をクリアすると、変数 c がクリアされます。どうすればそれを防ぐことができますか? 変数 c は、すべてのデータを保存した後にのみクリアする必要があります。

4

1 に答える 1

0

あなたが言おうとしていることを私が理解しているなら、はい、ハンドラーを使用して問題を解決する方法があります。

非同期タスクで、次のようにします-

   private mLocations;
 public MyTask(Handler mResponseHandler, List<Location> mLocations){
        super();
        this.mLocations = mLocations;
        this.mResponseHandler = mResponseHandler;
    }

onPostExecuteで、

  onPostExecute(List<Location>){

 @Override
    protected void onPostExecute(Boolean result) {

        super.onPostExecute(result);

        Log.i(TAG, "result = "+result);
        if (mResponseHandler == null) return;

        MyLocationData<Location> resultData = new MyLocationData<Location>();
        if(result != null && result){
            resultData.requestSuccessful = true;
            resultData.responseErrorCode = 0;
        }else{
            resultData.requestSuccessful = false;
            resultData.responseErrorCode = errorCode;  //set this when result is null
        }

        android.os.Message m = android.os.Message.obtain();
        m.obj = resultData;
        mResponseHandler.sendMessage(m);
    }
}

MyLocationData は、関連するすべてのデータを保存するモデル クラスです。このようなクラスになることができます -

 public class MyLocationData<Type> {

    public Type response;
    public int responseErrorCode;
    public boolean requestSuccessful;
}

アクティビティで、次のようなデータを取得できます。

private Handler mExportHandler = new Handler(){ 

        public void handleMessage(android.os.Message msg) {
                MyLocationData<Location> responseData = (MyLocationData<Location>) msg.obj;
                 // your logic for fetching new locations from responseData and using them in your activity   

        };
    };
于 2012-10-29T07:05:17.083 に答える