在onCreate之前将来自mySQL的信息放入Fragment textView中

时间:2015-05-01 03:10:28

标签: android mysql json android-fragments

我的应用程序当前加载片段,当我尝试在片段onCreate()期间加载人物的个人资料时,它会在片段制作完成后拉动,我的文本视图都不会改变。

日志:

  

04-30 23:03:37.522 5495-5495 / uconn.campusoddjobs D / I'在片段中:null   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 个答案:

没有答案