gpt4 book ai didi

java - 在 Android Studio 上使用 AsyncTask 感到困惑

转载 作者:行者123 更新时间:2023-12-01 11:06:25 25 4
gpt4 key购买 nike

我在开发 Android 应用程序时遇到一些问题。我想制作一个测验应用程序,让用户选择一个答案,该应用程序将其存储在一个数组中,当用户回答所有问题时,应用程序将计算所有用户选择的数量。

但是在这里我在使用 AsyncTask 保存答案时感到困惑,因为我不想使用问题框的 fragment ,并且我希望当用户单击“下一步”按钮时,用户的答案将存储在某个数组中异步任务方法。但在 doInBackground 中,我不知道如何从中返回数组并在 mainActivity 中捕获。

我很困惑,请高手帮助我。

之前谢谢。

注意。这是我的 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);
}

}

最佳答案

首先。您不需要 AsyncTask 来实现这一点。无论如何,您应该使用 onPostExecute 将结果返回到主线程中。 web 上有数千个如何使用 asynctask 的示例。

但同样,您甚至不需要为此使用异步任务。

关于java - 在 Android Studio 上使用 AsyncTask 感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32900120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com