尝试在片段中的AsyncTask中设置EditText中的文本。这在我的其他课程中效果很好,但片段让我失望。
@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;
}
}
任何帮助都会非常感激。我尝试将editText .setText放在onPostExecute中,但片段不允许它。我也试过返回值,但它只允许一个项目。
答案 0 :(得分:1)
尝试这种方法:
示例代码
public class FragmentA extends Fragment implements MyAsyncTask.OnDataFetchedListener
{
//do all your stuff here
private EditText text;
@Override
public View onCreateView( ....)
{
//get edit text views here
}
@Override
public void updatText(String[] data)
{
String firstValue = data[0];
text.setText(firstValue);
}
}
现在在您的异步任务中执行以下操作:
public class MyAsyncTask extends AsnycTask<String, String, String>
{
private String[] data;
@Override onPreExecute(){}
@Override protected String doInBackground(String ..)
{
//do whatever you need here and return whatever you need
//add your data to the list here
}
@Override
public void onPostExecute(String result)
{
//you can return a list of results here
try{
((OnDataFetchedListener), getActivity()).updateText(data);
}catch(ClassCastException)
{
}
}
public interface OnDataFetchedListener
{
void updateText(String[] data);l
}
}
我希望这会对你有所帮助。