【问题标题】:Put information from mySQL into a Fragment textView's before onCreate在 onCreate 之前将来自 mySQL 的信息放入 Fragment textView 中
【发布时间】: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


【解决方案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();

  }
}

我的帐户片段

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-15
    • 2020-08-19
    • 2017-08-20
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    相关资源
    最近更新 更多