Java 如何将变量从AsyncTask返回到Activity中的OnCreate() 问题/错误:
我正在努力将一个变量从doInBackground方法传递到OnCreate()。我真不敢相信我在这件事上有这么多问题 目标: 将字符串从doInBackground中的AsyncTask方法传递到OnCreate,我想将字符串传递到Textview。并使用字符串设置XtView 我的理解是: 我已经厌倦了在doInBackground&AsyncTask方法中创建简单方法,并在onCreate()中调用它。但是,该变量始终为空。我相信我没有理解onCreate()的一个方面 主要活动:-我想在textView中设置变量“ValueRequired” AsyncTask-包含doInBackgroundJava 如何将变量从AsyncTask返回到Activity中的OnCreate() 问题/错误:,java,android,variables,methods,android-asynctask,Java,Android,Variables,Methods,Android Asynctask,我正在努力将一个变量从doInBackground方法传递到OnCreate()。我真不敢相信我在这件事上有这么多问题 目标: 将字符串从doInBackground中的AsyncTask方法传递到OnCreate,我想将字符串传递到Textview。并使用字符串设置XtView 我的理解是: 我已经厌倦了在doInBackground&AsyncTask方法中创建简单方法,并在onCreate()中调用它。但是,该变量始终为空。我相信我没有理解onCreate()的一个方面 主要活动:-我想在
您必须在
onPostExecute
中执行,而不是在doInBackground
中执行。只需放入onPostExecute
textView.setText(“ValueNeeded”);
您的问题不是“理解onCreate()的一个方面”,而是“理解AsyncTask的一个方面”您必须在
onPostExecute
中完成,而不是在doInBackground
中。只需将其放入onPostExecute
textView.setText(“ValueRequired”)代码>
您的问题不是“理解onCreate()的一个方面”,而是“理解AsyncTask的一个方面”您的onCreate需要快速。AsyncTask的要点是在另一个线程中执行一些操作,以便onCreate可以运行
实现onPostExecute(…)并将其填入结果。您的onCreate可能需要有某种“Loading…”消息来向用户指示您正在获取数据 你的onCreate需要快一点。AsyncTask的要点是在另一个线程中执行一些操作,以便onCreate可以运行
protected String doInBackground(String... args) {
..CODE THAT GETS VALUE IS IN HERE...
//ValueNeeded Is
ValueNeeded = c.getString(TAG_TOUR);
// return your value needed here
return ValueNeeded;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
// this result parameter has the value what you return in doInBackground
// now it has valueNeeded
// set that value to your textview
textView.setText("ValueNeeded);
}
实现onPostExecute(…)并将其填入结果。您的onCreate可能需要有某种“Loading…”消息来向用户指示您正在获取数据 从doInBackground返回字符串并在onPostExecute()中设置它。onPostExecute()是绑定结果的地方。从doInBackground返回字符串并在onPostExecute()中设置。onPostExecute()是绑定结果的地方。
protected String doInBackground(String... args) {
..CODE THAT GETS VALUE IS IN HERE...
//ValueNeeded Is
ValueNeeded = c.getString(TAG_TOUR);
// return your value needed here
return ValueNeeded;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
// this result parameter has the value what you return in doInBackground
// now it has valueNeeded
// set that value to your textview
textView.setText("ValueNeeded);
}
protected String doInBackground(String... args)
{
..CODE THAT GETS VALUE IS IN HERE...
//ValueNeeded Is
ValueNeeded = c.getString(TAG_TOUR);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected String doInBackground(String... args) {
..CODE THAT GETS VALUE IS IN HERE...
//ValueNeeded Is
ValueNeeded = c.getString(TAG_TOUR);
// return your value needed here
return ValueNeeded;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
// this result parameter has the value what you return in doInBackground
// now it has valueNeeded
// set that value to your textview
textView.setText("ValueNeeded);
}