2015-05-01 44 views
0

我的應用程序當前加載片段,並且當我嘗試在片段onCreate()期間加載人員的配置文件時,片段製作完成後, textviews可以改變。將信息從mySQL放入片段textView的onCreate前

登錄:

04-30 23:03:37.522 5495-5495/uconn.campusoddjobs d /我在片段:空 04-30 23:03:37.772 5495-5908/uconn.campusoddjobs d /拉從MySQL:測試

代碼:

public class MyAccountFragment extends Fragment{ 
private TextView name; 
private static final String PROFILE_URL = "http://campusoddjobs.com/oddjobs/buildprofile.php"; 
private Profile profile = new Profile(); 
private String test = String.valueOf(profile.username()); 
View rootview; 
@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    rootview = inflater.inflate(R.layout.my_account_layout, container, false); 
    Log.d("I'm in the Fragment", test); 
    name = (TextView) rootview.findViewById(R.id.nameview); 
    name.setText("you don't change"); //the textView changes to this though. 
    return rootview; 
    } 
} 

我應該片段之前初始化他們的個人資料?如果是這樣的話?

編輯:拉動數據 代碼:

public class Profile extends Activity{ 

private int id; 
private String email; 
private String username; 
private String bio; 
private String posted_jobs; 
private String accepted_jobs; 
private int karma; 

public String getTest; 

private static final String PROFILE_URL = "http://campusoddjobs.com/oddjobs/buildprofile.php"; 
JSONparser jparser = new JSONparser(); 

public Profile() { 

    email = getEmailFromMemory(); 
    new buildProfile().execute(); 
} 

// ---------- Getters ---------- 

public String username(){return username;} 
--------------------------- 
// ---------- Setters ---------- 
public void setUsername(String u){username = u;} 
// ----------------------------- 

private String getEmailFromMemory() {   // pulls email from shared preferences 
    Context context = MainActivity.getAppContext(); 
    SharedPreferences prefs = context.getSharedPreferences("user_settings", Context.MODE_PRIVATE); 
    String extractedText = prefs.getString("email", "error: no email"); 
    return extractedText; 
} 

class buildProfile extends AsyncTask<String, String, String> { 
    public Profile getProfile() 
    { 
     return Profile.this; 
    } 

    @Override 
    protected String doInBackground(String... args) { 
     try { 
      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("email", email)); 

      JSONObject json = jparser.makeHttpRequest(PROFILE_URL, "GET", params); 

      Profile.this.setUsername(json.getString("username")); 
      Log.d("Pulled From mySQL", username()); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 
} 

}

+0

你從哪兒拉你的數據嗎?如果它是http調用,那麼自然會在結果和創建片段之間產生競爭。您應該發佈整個代碼,包括調用該網址的代碼。 – inmyth

+0

我從一個http調用拉,使用PHP獲取mySQL信息 –

回答

1

正如我所說的,有比賽的HTTP調用和片段創作之間正在進行。所以要解決它只是讓一個又一個開始。您有兩種選擇: - 在收到來自http請求的響應後實例化片段 - 先創建片段。創建後,觸發發送http請求的活動的回調。完成後更新片段。

你選擇哪一個應該取決於應用程序的設計。任何漫長的流程(如http呼叫)都需要進行臨時互動,以便用戶知道該應用沒有崩潰(即運行微調,動畫或在後臺執行)

這是第一種方法。我假設這個AsyncTask以合法的方式執行(通過Activity生命週期方法而不是建議的問題)。

class buildProfile extends AsyncTask<String, String, String> { 
    ... 

    @Override 
    protected String doInBackground(String... args) { 
    try { 
     // Building Parameters 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("email", email)); 

     JSONObject json = jparser.makeHttpRequest(PROFILE_URL, "GET", params); 

     String username = json.getString("username"); 
     Profile.this.setUsername(username); 
     return username; 

    } catch (JSONException e) { 
     e.printStackTrace(); 
     return null; 
    } 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 
     if (s == null){return;} 
     //this is where MyAccountFragment is created 
     getFragmentManager() 
     .beginTransaction() 
     .add(R.id.myaccountfragmentcontainer, MyAccountFragment .newInstance(s)) 
     .commit(); 

    } 
} 

MyAccountFragment

public class MyAccountFragment extends Fragment{ 
... 
private TextView name; 

    public static MyAccountFragment newInstance(String username) { 
    MyAccountFragment f = new MyAccountFragment(); 

    Bundle args = new Bundle(); 
    args.putString("username", username); 
    f.setArguments(args); 
    return f; 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
    rootview = inflater.inflate(R.layout.my_account_layout, container, false); 

    name = (TextView) rootview.findViewById(R.id.nameview); 

    // take username argument and put it in textview 
    Bundle args = getArguments(); 
    String username = args.getString("username"); 
    name.setText(username); 

    return rootview; 
    } 
}