views:

98

answers:

2

Hi!

I have a string that I want to update using AsyncTask class here is my code:

public class MainActivity extends Activity{

private String str = "oldString";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        getString();
        }
       private void getString(){
              new CustomTask().execute();
       }

     private class CustomTask extends AsyncTask<Void, Void, String>{

          @Override
          protected String doInBackground(Void... params) {
           // TODO Auto-generated method stub
           Log.i("Lesson3", "doInBackground method");
           str = "newString";
           return "someString";
          }

          protected void onPostExecute(String s){
           Log.i("Lesson3", "onPostExecute method");

          }

}

onPostExecute method is not called, thats the problem??

Thanks

A: 
public class MainActivity extends Activity {

    private String str = "oldString";

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

        getString();
    }

    private void getString() {
        new CustomTask().execute();
    }

    private void postExecuteCallback() {
        Log.i("Lesson3", "String after callback: " + str );
    }

    private class CustomTask extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... params) {
            // TODO Auto-generated method stub
            Log.i("Lesson3", "doInBackground method");
            str = "newString";
            return "someString";
        }

        protected void onPostExecute(String s) {
            Log.i("Lesson3", "onPostExecute method");
            postExecuteCallback();
        }
    }
}
Bakapii
it's just a syntax error waht I commited then I rewrite code here, it's run fine bun the value of the string remains "oldString" it doesn't changed
Maki
Changed the code, it does correctly change the 'str' variable properly now.
Bakapii
A: 

Why have you decided it doesn't run? Following code works just fine (fixed syntax errors in yours):

package com.stackoverflow;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class Test extends Activity {

private String str = "oldString";

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

    getString();
}

private void getString() {
    new CustomTask().execute();
}

private class CustomTask extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        // TODO Auto-generated method stub
        Log.i("Lesson3", "doInBackground method");
        str = "newString";
        return "someString";
    }

    protected void onPostExecute(String s) {
        Log.i("Lesson3", "onPostExecute method");

    }

}

}

Konstantin Burov