Confuse Using AsyncTask on Android Studio

here i have some problem when developing an android app. I want to make a quiz app which let user select an answer, the app store it inside an array and app will count all the user select when all question has been answered by user.

But here i have confuse when using AsyncTask to save the answer, because i doesn't want to use fragment for the question box, and i want when the user click Next Button, the answer of user will stored in some array with AsyncTask method. But here inside the doInBackground i have no idea how to return the array from it and catch inside the mainActivity.

I'm very confuse, please master help me.

Thanks Before.

NB. this my code for MainActivity.java (which contain AsyncTask method)

public class MainActivity extends ActionBarActivity {
Button btnKet1 = (Button)findViewById(R.id.btn1);
Button btnKet2 = (Button)findViewById(R.id.btn2);
Button btnKet3 = (Button)findViewById(R.id.btn3);
Button btnKet4 = (Button)findViewById(R.id.btn4);
Button btnNext = (Button)findViewById(R.id.btnNext);
TextView txtKet = (TextView)findViewById(R.id.txtKeterangan);
RadioButton rd1 = (RadioButton)findViewById(R.id.rd1);
RadioButton rd2 = (RadioButton)findViewById(R.id.rd2);
RadioButton rd3 = (RadioButton)findViewById(R.id.rd3);
RadioButton rd4 = (RadioButton)findViewById(R.id.rd4);
public static String answer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    onButtonClickListener();
}

private void onRadioButtonClick(){
    if(rd1.isChecked()){
        answer = "1";
    }else if(rd2.isChecked()){
        answer = "2";
    }else if (rd3.isChecked()){
        answer = "3";
    }else if (rd4.isChecked()){
        answer = "4";
    }else{
        answer = "0";
    }
}
private void onButtonClickListener(){
    btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        onRadioButtonClick();
            AsyncTaskRunner runner = new AsyncTaskRunner();
            runner.execute(answer);

        }
    });

    btnKet1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    btnKet2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    btnKet3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    btnKet4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
}

private class AsyncTaskRunner extends AsyncTask<String, String, Integer[]>{
    private int[] answerpasti = new int[40];
    Integer answertemp = Integer.parseInt(answer);
    String respon;

    @Override
    protected Integer[] doInBackground(String... params) {
        publishProgress("Antos Jep Kleng"); // Calls onProgressUpdate()
        try {
            for(int i = 0;i<40;i++){
                answerpasti[i]=answertemp;
            }
        } catch (Exception e) {
            e.printStackTrace();
            respon = e.getMessage();
        }
        return answerpasti[];
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

Answers


First of all. You don't need and AsyncTask for that. Anyways you should use onPostExecute to get your result back in the main thread. There are thousand of examples of how to use an asynctask on the web

But again, you don't even need an async task for that.


To return a result from an AsyncTask you should either use the method onProgressUpdate for partial results, or the method onPostExecute to get the results after the async task finished. So if your method doInBackground returns an integer array, your onPostExecuteMethod will get that array as a parameter and then you should store it in a property of your MainActivity

public class MainActivity extends ActionBarActivity{
  //property to store the result of the asyncTask
  private Integer []arr;
  private class AsyncTaskRunner extends AsyncTask<String, String, Integer[]>{

    @Override
    protected Integer[] doInBackground(String... params) {
      ...
    }
   }
   @Override
   protected void onPostExecute(Integer[]result) {
     MainActivity.this.arr=result;
   }
}

More info here


Need Your Help

Records in python "keyword can't be an expression" namedtuple

python

I am learning about records in my computing class, I understand that they are like a record in a data base. I was told to use namedtuples for this. But my code isn't working. I get a "keyword can't...

Issue accessing a relation in a PHP object

php oop laravel-4

I am using Laravel 4 and have this line: