【问题标题】:Using AsyncTask to get data in a Fragment使用 AsyncTask 获取 Fragment 中的数据
【发布时间】:2014-08-27 21:53:56
【问题描述】:

尝试在我的片段中的 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 中,但片段不允许这样做。我也尝试返回值,但它只允许一项。

【问题讨论】:

    标签: android android-fragments android-asynctask


    【解决方案1】:

    试试这个方法:

    • 使用方法在异步任务中创建接口。
    • 在您的活动中实现该接口。覆盖方法
    • 现在在你的 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
      }
    }
    

    希望对你有帮助。

    【讨论】:

    • 我无法实现这个新类。 Eclipse 一直想创建一个具有该名称的新类。我确定这是我的错误!在这篇文章stackoverflow.com/questions/11833978/… 的帮助下,我能够在片段中填充我的 EditText 和 TextViews 通过添加一个包装类并将其作为参数放在 asynctask create 中,我能够在 OnPostExecute 中返回它的值并设置这些值。感谢您的所有帮助,我仍然相信您的回答是正确的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多