我的應用程序當前加載片段,並且當我嘗試在片段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;
}
}
}
你從哪兒拉你的數據嗎?如果它是http調用,那麼自然會在結果和創建片段之間產生競爭。您應該發佈整個代碼,包括調用該網址的代碼。 – inmyth
我從一個http調用拉,使用PHP獲取mySQL信息 –