2016-01-14 69 views
1

我想從活動發送數據到片段,但在片段中,我得到null而不是我的值,以下是我的代碼可以幫助我嗎?捆綁不工作

String nofrndthere="nofrnds"; 
      String frndthere="frnds"; 
      if(jsonary.length()==0) 
      { 
       // Toast.makeText(MainActivity.this,"Null",Toast.LENGTH_SHORT).show(); 

       Bundle bundle = new Bundle(); 
       bundle.putString("nofrndsavailable", nofrndthere); 
       HomeFragment fragobj = new HomeFragment(); 
       fragobj.setArguments(bundle); 
      } 
      else if(jsonary.length()!=0) 
      { 
       // Toast.makeText(MainActivity.this,"Not Null",Toast.LENGTH_SHORT).show(); 
       Bundle bundle = new Bundle(); 
       bundle.putString("frndsavailable", frndthere); 
       HomeFragment fragobj = new HomeFragment(); 
       fragobj.setArguments(bundle); 
      } 

HomeFragment

public class HomeFragment extends Fragment { 

    ExpandableListAdapter listAdapter; 
    ExpandableListView expListView; 
    List<String> listDataHeader; 
    HashMap<String, List<String>> listDataChild; 

    private FragmentTabHost tabHost; 
    private SearchView searchView; 
    private String strtext; 
    private String strtextss,strtextfnrdval; 


    public HomeFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     tabHost = new FragmentTabHost(getActivity()); 
     tabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_parent_fragment); 


     Bundle bundle = this.getArguments(); 
     strtext = bundle.getString("user_login_id"); 
     if (getArguments() != null) { 
      strtextss = bundle.getString("nofrndsavailable"); 
      strtextfnrdval= bundle.getString("frndsavailable"); 

     } 
     System.out.println("DATASSSSS : " +strtextss+strtextfnrdval); 

     System.out.println("<<<<<<<<<<<<<< Session ID : " + strtext+strtextss+strtextfnrdval); 



     Bundle arg1 = new Bundle(); 
     arg1.putInt("Arg for Frag1", 1); 
     if(strtext!=null) { 
      arg1.putString("user_logid", strtext); 
     } 

     tabHost.addTab(tabHost.newTabSpec("one").setIndicator(getTabIndicator(tabHost.getContext(), R.drawable.icon_profile_tabs,"Home")), DiscoverFragment.class, arg1); 
     Bundle arg2 = new Bundle(); 

     arg2.putInt("Arg for Frag2", 2); 

     if(strtext!=null) { 
      arg2.putString("user_logid_sectab", strtext); 
     } 

     tabHost.addTab(tabHost.newTabSpec("Sec"). 
       setIndicator(getTabIndicator(tabHost.getContext(), R.drawable.icon_frnds_tab,"Invite")), ShopFragment.class, arg2); 


     Bundle arg3 = new Bundle(); 
     arg3.putInt("Arg for Frag3", 3); 

     if(strtext!=null) { 
      arg3.putString("user_logid_thirdtab", strtext); 
     } 



     tabHost.addTab(tabHost.newTabSpec("Third"). 
       setIndicator(getTabIndicator(tabHost.getContext(), R.drawable.icon_wish_tab,"Wish")), Thirdtab.class, arg3); 


     Bundle arg4 = new Bundle(); 
     arg4.putInt("Arg for Frag4", 4); 
     if(strtext!=null) { 
      arg4.putString("user_logid_fourthtab", strtext); 
     } 


     if(strtextss == getArguments().getString("nofrndsavailable")) 
     { 
      // Toast.makeText(getActivity(), "Null in home", Toast.LENGTH_SHORT).show(); 
      System.out.println("Null in home"); 
      tabHost.addTab(tabHost.newTabSpec("Four"). 
        setIndicator(getTabIndicator(tabHost.getContext(), R.drawable.icon_notification_tab, "Alert")), FourthTabs.class, arg4); 

     } 
     else if(strtextfnrdval == getArguments().getString("frndsavailable")) 
     { 
      // Toast.makeText(getActivity(), "Not Null in home", Toast.LENGTH_SHORT).show(); 
      System.out.println("NotNull in home"); 
      tabHost.addTab(tabHost.newTabSpec("Four"). 
        setIndicator(getTabIndicator(tabHost.getContext(), R.drawable.icon_fnrdalert, "Alert")), FourthTabs.class, arg4); 

     } 

     return tabHost; 


    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
    } 

    public View getTabIndicator(Context context, int icon,String text) { 
     View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null); 
     ImageView iv = (ImageView) view.findViewById(R.id.indicatorImageView); 
     iv.setImageResource(icon); 
     TextView txt = (TextView) view.findViewById(R.id.txttb); 
     txt.setText(text); 
     return view; 
    } 



} 
+0

你可以告訴我試圖獲得'Fragment'的哪個方法getArguments()。getString(「」);' –

+0

哪些值爲空? getArguments()是否已經返回null? –

+0

@BenjaminScharbau查看我編輯的問題 –

回答

1

打開片段之前,你這是怎麼能捆綁附加到它。以下代碼應該放在您的活動中以將該包放入片段中。

Fragment fragment = new YourFragmentClass(); 
Bundle args = new Bundle(); 

args.putString(ARG_PARAM1, param1); 
args.putString(ARG_PARAM2, param2); 
fragment.setArguments(args); 

現在您已經附加了捆綁打開碎片。

你必須做的事情是在你的片段中重寫onCreate()方法,這就是你將如何收到這個包。

private String mParam1; // these are global variable to get the data 
private String mParam2; // after you receive the data, it can be used under `onActivityCreated()` 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if (getArguments() != null) { 
     mParam1 = getArguments().getString(ARG_PARAM1,null); 
     mParam2 = getArguments().getString(ARG_PARAM2,null); 
// I have updated the code thisis how you can check which string is empty. 
// cause if you try to do some work on null object you will get error 
     if(mParam1 == null){ 
      // it is empty 
     }else{ 
      // it is not empty 
     } 
     if(mParam2 == null){ 
      // it is empty 
     }else{ 
      // it is not empty 
     } 
    } 
} 

@Override 
public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    // here you can use mParam1 and mParam2 
} 

更新 與交談,我與你,你就可以開始你的片段,雖然你這樣的活動告訴片段,如果數組爲空或不是

HomeFragment fragobj = new HomeFragment(); 

Bundle bundle = new Bundle(); 
if(jsonary.length()==0){ 
bundle.putBoolean("isAvailable", false); 
}else{ 
bundle.putBoolean("isAvailable", true); 
} 

fragobj.setArguments(bundle); 

,並在你的片段的onCreate()方法這是你如何閱讀布爾值

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if (getArguments() != null) { 
     boolean mParam1 = getArguments().getBoolean("isAvailable",false); 
    } 
} 

更新

確定現在我知道你在做什麼(片段設置集合前已經開始了),你可以試試這個。 (這是相當無恥的)

使你的方法HomeFragment

public static void setData(boolean isAvailable){ 
// here you will get the actual data 
} 

現在裏面你的活動時,你得到的JSONArray取決於你能,你可以做到這一點。

HomeFragment.setData(true/false) 
+0

查看我編輯的問題 –

+0

@Lakhan,mate,您正在爲您的活動分配兩個不同的包到兩個不同的片段對象,這就是爲什麼當您啓動片段時,只有一個字符串可用。因爲在給定的時間,無論你啓動哪個片段,它只有一個字符串 –

+0

這是兩個diff assigining的區別嗎? –

0

從你的問題,我認爲你正在user_login_id爲空從包

Bundle bundle = this.getArguments(); 
strtext = bundle.getString("user_login_id"); 

這是因爲你還沒有把任何字符串user_login_id組合中的

if(jsonary.length()==0) 
    { 
     // Toast.makeText(MainActivity.this,"Null",Toast.LENGTH_SHORT).show(); 

     Bundle bundle = new Bundle(); 
     bundle.putString("nofrndsavailable", nofrndthere); 
     bundle.putString("user_login_id", <userid>); //put user id here 

     HomeFragment fragobj = new HomeFragment(); 
     fragobj.setArguments(bundle); 
    } 
    else if(jsonary.length()!=0) 
    { 
      // Toast.makeText(MainActivity.this,"Not Null",Toast.LENGTH_SHORT).show(); 
      Bundle bundle = new Bundle(); 
      bundle.putString("frndsavailable", frndthere); 
      bundle.putString("user_login_id",<userid>);// put user id here 
      HomeFragment fragobj = new HomeFragment(); 
      fragobj.setArguments(bundle); 
    }