问题描述
我可以用一个变量的值存储。现在我想该变量传递到一个片段
用下面的code,我能够加载片段:
I can store the value in one variable. Now I want pass that variable into a fragmentUsing the code below, I am able to load fragments:
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(对于列表视图)和一个用于片段。
The output is splitted into 2 windows one for extend for listfragment (for listview) and one for fragments.
Fragment2.xml
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 ..请帮助我
AndroidListFragmentActivity in this class itemname I want pass Fragment2.class.. please help me
推荐答案
如果这两个片段是在其他活动则可以使用意图
If both fragment are on other activity then can use intent
如果在相同的活动然后可以对特定的活动做了手术。
if on same activity then can do operation on that particular activity
在本
看到类TitlesFragment的onListItemClick
see onListItemClick of class TitlesFragment
**
* 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);
}
这篇关于如何通过数据之间的活动在Android中使用束片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!