2014-08-27 59 views
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放入,但片段不允許。我也嘗試返回值,但它只允許一個項目。

回答

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 
    } 
} 

我希望這可以幫助你。

+1

我從來沒有能夠實現新的類。 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