【发布时间】:2015-07-10 22:43:57
【问题描述】:
我的应用程序当前加载片段,当我尝试在片段 onCreate() 期间加载人的个人资料时,它最终会在片段制作完成后拉动,并且我的任何文本视图都无法更改。
日志:
04-30 23:03:37.522 5495-5495/uconn.campusoddjobs D/我在片段中:null 04-30 23:03:37.772 5495-5908/uconn.campusoddjobs D/Pulled From 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 调用,那么结果和片段的创建之间自然会存在竞争。您应该发布整个代码,包括调用该 url 的代码。
-
我正在从一个 http 调用中提取,它使用 PHP 来获取 mySQL 信息
标签: android mysql json android-fragments