[英]How to pass data from an activity to a fragment on android
我有一個名為“家庭”的活動; 登錄后,我被重定向到此活動。 此活動以這種方式實現了兩個片段(頁眉和頁腳)。
View header = inflater.inflate(R.layout.fragment_header_fragement, null);
View footer = inflater.inflate(R.layout.fragment_footer, null);
mDrawerListView.addHeaderView(header);
mDrawerListView.addFooterView(footer);
我想主要顯示在實現到“ Home”活動中的標頭片段中,這些數據是從日志記錄活動中傳輸的。 我搜索了一下這個問題,發現我們只能在主要活動中添加或替換一個片段,而不能在另一個片段中添加或替換一個片段。 我試圖資助解決方案,但還沒有。 這是我的代碼:
@Override
protected void onPostExecute(final String res) {
//LoginTask = null;
//showProgress(false);
try {
jObj = new JSONObject(res);
} catch (JSONException e) {
e.printStackTrace();
}
try {
if (jObj.getString("code").equals("1")) {
infos = new Bundle();
infos.putString("ID",jObj.getString("ID"));
infos.putString("Name",jObj.getString("display_name"));
infos.putString("ImgUrl","http://unchained-network.com/uploads/profilpics/53f5c570b6ac2.png");
android.support.v4.app.FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
HeaderFragement hf = new HeaderFragement();
//Charger les infos dans l'activité
hf.setArguments(infos);
tr.add(R.id.frgmnt,hf);
tr.commit();
Intent myIntent = new Intent(getActivity(), HomesActivity.class);
//Lançer l'activité
startActivityForResult(myIntent, 0);
} else {
//password.setError(getString(R.string.error_incorrect_password));
//password.requestFocus();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Error !");
builder.setMessage("The information entered is incorrect.\nPlease try again!")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
這就是我將數據提取到片段中的方式
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
name = (TextView) getView().findViewById(R.id.nameH);
img = (ImageView) getView().findViewById(R.id.imageViewHeader);
Bundle infos = getArguments();
name.setText(infos.getString("Name"));
img.setImageBitmap(getBitmapFromURL(infos.getString("ImgUrl")));
}
因此,請采取任何解決方案。 謝謝
您為什么不僅僅通過意圖傳遞數據? 您有兩個活動Login
和Home
。
Intent myIntent = new Intent(getActivity(), HomesActivity.class);
//Lançer l'activité
startActivityForResult(myIntent, 0);
在上面的代碼中,您可以像這樣簡單地添加額外的信息
myIntent.putExtra("name", "value");
然后您可以在“家庭活動”中檢索此內容
String extra = i.getStringExtra("name");
然后將這些額外的內容傳遞給您的標頭片段。
將變量傳遞給Fragment的代碼沒有任何問題。 我剛剛復制了您的代碼,效果很好。
活動:
Bundle infos = new Bundle();
infos.putString("ID", "1");
infos.putString("Name", "Gonzo");
FragmentMain frag = new FragmentMain();
frag.setArguments(infos);
分段:
Bundle infos = getArguments();
Log.i(getTag(), infos.getString("Name"));
我建議您將一些日志記錄放入onPostExecute中,或逐步進入調試模式。 確保您的json返回了您期望的結果。
作為一般建議,infos.getString接受兩個參數,第二個是默認值,可能有用。 還要避免使用硬編碼鍵,例如“名稱”,而要使用公共靜態字符串並引用它。
祝好運。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.