Android 使用AsyncTask获取片段中的数据

Android 使用AsyncTask获取片段中的数据,android,android-fragments,android-asynctask,Android,Android Fragments,Android Asynctask,正在尝试从我的片段中的AsyncTask设置EditText中的文本。这在我的其他课程中效果很好,但是这个片段让我很反感 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.profile_fragment_main, c

正在尝试从我的片段中的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;
    }
}
@覆盖
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(右布局剖面图、碎片图、主容器、,
假);
新建getinfo().execute();
如果(emailTwo!=null){
email2.setText(emailTwo);
} 
如果(emailThree!=null){
email3.setText(emailThree);
}
如果(mailingaddress1!=null){
mail1.setText(mailingaddress1);
}
}//onCreate结束
类getinfo扩展了异步任务{
//onPreExecute()和onPostExecute
@凌驾
受保护的字符串doInBackground(字符串…args){
成功;
试一试{
List params=new ArrayList();
添加(新的BasicNameValuePair(“用户名”,restoredemail));
JSONObject json=jsonParser.makeHttpRequest(登录地址,“POST”,
参数);
success=json.getInt(TAG_success);
如果(成功==2){
emailTwo=json.getString(“secondemail”);
emailThree=json.getString(“thirdemail”);
mailingaddress1=json.getString(“mailingaddress1”);
返回json.getString(TAG_消息);
}否则{
Log.d(“登录失败!”,json.getString(TAG_MESSAGE));
返回json.getString(TAG_消息);
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
}
任何帮助都将不胜感激。我尝试将editText.setText放在onPostExecute中,但片段不允许这样做。我还尝试返回值,但它只允许一项

试试这种方法:

  • 使用方法在异步任务中创建接口
  • 在活动中实现该接口。重写该方法
  • 现在在onPostExecute中,调用接口方法来通知活动
  • 在该方法中(在活动中),只需通过调用片段的方法来通知片段,该方法只需在EditText字段中设置文本即可
示例代码

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
  }
}
公共类MyAsyncTask扩展AsnycTask
{
私有字符串[]数据;
@重写onPreExecute(){}
@覆盖受保护字符串doInBackground(字符串..)
{
//在这里你需要什么就做什么,需要什么就回来什么
//将您的数据添加到此处的列表中
}
@凌驾
public void onPostExecute(字符串结果)
{
//您可以在此处返回结果列表
试一试{
((OnDataFetchedListener),getActivity()).updateText(数据);
}捕获(ClassCastException)
{
}
}
公共接口OnDataFetchedListener
{
void updateText(字符串[]数据);l
}
}

我希望这对您有所帮助。

我从未实现过新类。Eclipse总是希望创建一个具有该名称的新类。我确信这是我的错误!在这篇文章的帮助下,我能够在片段中填充我的EditText和TextView,方法是添加一个包装器类,并将其作为asynctask创建中的参数。我能够在OnPostExecute中返回它的值并设置值。谢谢你的帮助,我仍然相信你的答案是正确的。