0
嘗試從片段中的AsyncTask中設置EditText中的文本。這在我的其他課程中非常有用,但是Fragment正在拋棄我。使用AsyncTask獲取片段中的數據
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.profile_fragment_main, container,
false);
new getinfo().execute();
if (emailTwo != null) {
email2.setText(emailTwo);
}
if (emailThree != null) {
email3.setText(emailThree);
}
if (mailingaddress1 != null) {
mail1.setText(mailingaddress1);
}
} // end of onCreate
class getinfo extends AsyncTask<String, String, String> {
// onPreExecute() and onPostExecute here
@Override
protected String doInBackground(String... args) {
int success;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", restoredemail));
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
params);
success = json.getInt(TAG_SUCCESS);
if (success == 2) {
emailTwo = json.getString("secondemail");
emailThree = json.getString("thirdemail");
mailingaddress1 = json.getString("mailingaddress1");
return json.getString(TAG_MESSAGE);
} else {
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
任何幫助將非常感激。我試着把onTextExecute中的editText .setText放入,但片段不允許。我也嘗試返回值,但它只允許一個項目。
我從來沒有能夠實現新的類。 Eclipse總是想用這個名字創建一個新類。我相信這是我的錯誤!我能夠在這篇文章的幫助下填充我的EditText和TextViews在這篇文章的幫助下http://stackoverflow.com/questions/11833978/asynctask-pass-two-or-more-values-from-doinbackground-to-onpostexecute By添加一個包裝類,並將其作爲參數在asynctask中創建我可以在OnPostExecute中返回它的值並設置值。感謝您的所有幫助,我仍然相信您的答案是實現這一目標的正確方法。 – JeffK 2014-08-31 21:24:27