简体   繁体   English

混淆在Android Studio上使用AsyncTask

[英]Confuse Using AsyncTask on Android Studio

here i have some problem when developing an android app. 在这里,我在开发Android应用程序时遇到了一些问题。 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. 但是这里我使用AsyncTask保存答案时感到困惑,因为我不想在问题框中使用片段,并且我希望当用户单击“下一步”按钮时,用户的答案将使用AsyncTask方法存储在某个数组中。 But here inside the doInBackground i have no idea how to return the array from it and catch inside the mainActivity. 但是在doInBackground内部,我不知道如何从中返回数组并捕获到mainActivity内部。

I'm very confuse, please master help me. 我很困惑,请高手帮帮我。

Thanks Before. 谢谢之前。

NB. 注意 this my code for MainActivity.java (which contain AsyncTask method) 这是我的MainActivity.java代码(包含AsyncTask方法)

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);
}

} }

First of all. 首先。 You don't need and AsyncTask for that. 您不需要使用AsyncTask。 Anyways you should use onPostExecute to get your result back in the main thread. 无论如何,您应该使用onPostExecute将结果返回到主线程中。 There are thousand of examples of how to use an asynctask on the web 有上千个有关如何在网络上使用asynctask的示例

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. 要从AsyncTask返回结果,您应该使用onProgressUpdate方法获取部分结果,或者使用onPostExecute方法在异步任务完成后获取结果。 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 因此,如果您的方法doInBackground返回一个整数数组,则onPostExecuteMethod将获得该数组作为参数,然后应将其存储在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 更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM