2012-06-09 88 views
1

我可以將值存儲在一個變量中。現在我想的是變量傳遞到片段 用下面的代碼,我能夠加載片段:如何使用Android中的包在活動之間傳遞數據片段

public class AndroidListFragmentActivity extends Activity { 
    Fragment2 f2; 
    public static String itemname; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.apetiserfragement); 
     itemname=getIntent().getStringExtra("itemname"); 
     Bundle args=new Bundle(); 
     args.putString("itemname", itemname); 
     f2=new Fragment2(); 
     f2.setArguments(args); 
    } 
} /* (Here I load fragment using xml page) itemname */ 

輸出被分裂成2個窗口中的一個用於延長listfragment(對於列表視圖),一個用於片段。

Fragment2.xml

public class Fragment2 extends Fragment { 
    String itemname; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     System.out.println(getArguments().getString("itemname")); 

     return inflater.inflate(R.layout.fragment2, container, false); 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
    } 
} 

AndroidListFragmentActivity在這個類ITEMNAME我想通過Fragment2.class ..請幫我

+0

參見此處這樣的響應:[數據共享beetween片段和活性androidi] [1] [1 ]:http://stackoverflow.com/questions/13445594/data-sharing-between-fragments-and-activity-in-android/20521851#20521851 –

回答

3

如果兩個片段是在其他活動,則可以使用意圖

如果然後可以對該特定活動進行操作

如在此link

看到類TitlesFragment的onListItemClick

** 
    * Helper function to show the details of a selected item, either by 
    * displaying a fragment in-place in the current UI, or starting a 
    * whole new activity in which it is displayed. 
    */ 
    void showDetails(int index) { 
     mCurCheckPosition = index; 

     if (mDualPane) {//<---------------------f on same activity then can do operation on that particular fragment 
      // We can display everything in-place with fragments, so update 
      // the list to highlight the selected item and show the data. 
      getListView().setItemChecked(index, true); 

      // Check what fragment is currently shown, replace if needed. 
      DetailsFragment details = (DetailsFragment) //<------------------------see use getFragmentManager 
        getFragmentManager().findFragmentById(R.id.details); 
      if (details == null || details.getShownIndex() != index) { 
       // Make new fragment to show this selection. 
       details = DetailsFragment.newInstance(index); 

       // Execute a transaction, replacing any existing fragment 
       // with this one inside the frame. 
       FragmentTransaction ft = getFragmentManager().beginTransaction(); 
       ft.replace(R.id.details, details); 
       ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
       ft.commit(); 
      } 

     } else { //<----------If both fragment are on other activity then can use intent 
      // Otherwise we need to launch a new activity to display 
      // the dialog fragment with selected text. 
      Intent intent = new Intent(); 
      intent.setClass(getActivity(), DetailsActivity.class); 
      intent.putExtra("index", index); 
      startActivity(intent); 
     } 
+0

我使用意圖。但它的申請強制退出..請幫助 – naveen

+1

請提供日誌貓和完整代碼的兩個片段........... –

+0

你又發佈了它? :) –

相關問題