我有一个包含不同片段的导航抽屉(每个项目显示一个片段),我想将数据从一个片段传递到另一个片段.(我只有一个活动,其中包含一个导航抽屉,其中有一个片段列表). .
我尝试了许多解决方案(捆绑和意图),但无法解决我的问题.
谁能帮助我.
这是我的第一个片段A:
包com.tutecentral.navigationdrawer;
import androID.app.Fragment;import androID.content.Context;import androID.content.Intent;import androID.content.SharedPreferences;import androID.os.Bundle;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.AdapterVIEw;import androID.Widget.AdapterVIEw.OnItemSelectedListener;import androID.Widget.ArrayAdapter;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.Spinner;import androID.Widget.TextVIEw;public class setting extends Fragment implements OnItemSelectedListener, VIEw.OnClickListener{button button_valIDate;EditText editText;Spinner spinner;@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { // Todo auto-generated method stub VIEw vIEw; vIEw=inflater.inflate(R.layout.setting, container, false); spinner=(Spinner) vIEw.findVIEwByID(R.ID.spinnerSetting); ArrayAdapter adapter=ArrayAdapter.createFromresource(getActivity(), R.array.ListMenu, androID.R.layout.simple_spinner_item); spinner.setAdapter(adapter); spinner.setonItemSelectedListener(this); editText=(EditText) vIEw.findVIEwByID(R.ID.price); button_valIDate=(button) vIEw.findVIEwByID(R.ID.buttonValIDatePrices); button_valIDate.setonClickListener(this); return vIEw;}@OverrIDepublic voID onItemSelected(AdapterVIEw<?> adapter, VIEw vIEw, int position,long arg3) { // Todo auto-generated method stub TextVIEw myText=(TextVIEw) vIEw; switch (position) { case 0: editText.setText("0"); break; case 1: editText.setText("1"); break; case 2: editText.setText("2"); break; case 3: editText.setText("3"); break; case 4: editText.setText("4"); break; case 5: editText.setText("5"); break; case 6: editText.setText("6"); break; case 7: editText.setText("7"); break; case 8: editText.setText("8"); break; case 9: editText.setText("9"); break; default: editText.setTag(getResources().getString(R.string.Default_prices)); }}@OverrIDepublic voID onnothingSelected(AdapterVIEw<?> arg0) { // Todo auto-generated method stub}@OverrIDepublic voID onClick(VIEw vIEw) { // Todo auto-generated method stub if(vIEw.getID()==R.ID.buttonValIDatePrices) { //SharedPreferences preferences=this.getActivity().getSharedPreferences("MyData", Context.MODE_PRIVATE); //SharedPreferences.Editor editor=preferences.edit(); //editor.remove("price_"+spinner.getSelectedItem().toString()); //editor.putString("price_"+spinner.getSelectedItem().toString(), editText.getText().toString()); //editor.commit(); Bundle bundle=new Bundle(); bundle.putString("price_"+spinner.getSelectedItem().toString(), editText.getText().toString()); Menu menu=new Menu(); menu.setArguments(bundle); }}
}
这是我应该在其中获取数据的FragmentB(请参阅getarguments):
包com.tutecentral.navigationdrawer;
/ **
*一个简单的{@link androID.support.v4.app.Fragment}子类.
*
* /
公共类Menu扩展Fragment实现OnClickListener,TabListener {
public Menu(){ // Todo auto-generated constructor stub}public static final String Default="20";public static Actionbar actionbar;button order, add, remove;gallery gallery;ImageVIEw imageVIEw;TextVIEw descriptionTextVIEw;TextVIEw textVIEwPrice;TextVIEw totalPrice;Bundle args = new Bundle();int total=0, price=0;@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container,Bundle savedInstanceState) { // Inflate the layout for this fragment VIEw vIEw=inflater.inflate(R.layout.fragment_menu, container, false); actionbar=getActivity().getActionbar(); actionbar.setNavigationMode(actionbar.NAVIGATION_MODE_TABS); order=(button) vIEw.findVIEwByID(R.ID.commander); order.setonClickListener(this); add=(button) vIEw.findVIEwByID(R.ID.addMenu); add.setonClickListener(this); remove=(button) vIEw.findVIEwByID(R.ID.removeMenu); remove.setonClickListener(this); gallery=(gallery)vIEw.findVIEwByID(R.ID.gallery); imageVIEw=(ImageVIEw)vIEw.findVIEwByID(R.ID.imageVIEw); gallery.setAdapter(new ImageAdapter(getActivity(), 0)); descriptionTextVIEw=(TextVIEw)vIEw.findVIEwByID(R.ID.descriptionText); textVIEwPrice=(TextVIEw) vIEw.findVIEwByID(R.ID.priceMenu); textVIEwPrice.setonClickListener(this); totalPrice=(TextVIEw) vIEw.findVIEwByID(R.ID.TotalFIEldMenu); price=Integer.parseInt(textVIEwPrice.getText().toString()); gallery.setonItemClickListener(new OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> adapter, VIEw arg1, int position,long arg3) { // Todo auto-generated method stub String price1; SharedPreferences preferences=getActivity().getSharedPreferences("MyData", Context.MODE_PRIVATE); switch (actionbar.getSelectednavigationIndex()) { case 0: imageVIEw.setimageResource(ImageAdapter.ThumbsIDs[position]); break; case 1: imageVIEw.setimageResource(ImageAdapter.ThumbsIDs2[position]); break; case 2: imageVIEw.setimageResource(ImageAdapter.ThumbsIDs3[position]); break; default: imageVIEw.setimageResource(ImageAdapter.ThumbsIDs[position]); break; } ChangeData(position); switch (ImageAdapter.ThumbsIDs[position]) { case R.drawable.pizza: //price1=preferences.getString("price_"+"pizza", Default); Log.i("bundlebundlebundle", getArguments().getString("price_"+"pizza",Default)); price1=getArguments().getString("price_"+"pizza", Default); break; case R.drawable.fishsoup: price1=preferences.getString("price_"+"Fish soup", Default); break; case R.drawable.frenchfrIEs: price1=preferences.getString("price_"+"french frIEs", Default); break; case R.drawable.chakhchoukha: price1=preferences.getString("price_"+"Chakhchoukha", Default); break; case R.drawable.couscous: price1=preferences.getString("price_"+"couscous", Default); break; case R.drawable.zlabia: price1=preferences.getString("price_"+"zlabia", Default); break; case R.drawable.juice: price1=preferences.getString("price_"+"juice", Default); break; case R.drawable.salade_fruit: price1=preferences.getString("price_"+"salade fruit", Default); break; case R.drawable.salade_fromage: price1=preferences.getString("price_"+"salade fromage", Default); break; case R.drawable.salade_verte: price1=preferences.getString("price_"+"salade verte", Default); break; default: price1=preferences.getString("price_"+"pizza", Default); break; } if(price1.equals(String.valueOf(Default))){ textVIEwPrice.setText(String.valueOf(Default)); } else{ textVIEwPrice.setText(price1); } price=Integer.parseInt(price1); } }); Actionbar.Tab tab1=actionbar.newTab(); tab1.setText(getResources().getString(R.string.Starter)); tab1.setTabListener(this); Actionbar.Tab tab2=actionbar.newTab(); tab2.setText(getResources().getString(R.string.Maindisk)); tab2.setTabListener(this); Actionbar.Tab tab3=actionbar.newTab(); tab3.setText(getResources().getString(R.string.Desert)); tab3.setTabListener(this); actionbar.addTab(tab1); actionbar.addTab(tab2); actionbar.addTab(tab3); return vIEw;}public Actionbar getActionbar() { return actionbar;}public voID setActionbar(Actionbar actionbar) { this.actionbar = actionbar;}//This methode allow us to change the data according to the index of ListVIEw.public voID ChangeData(int index){ String [] description=getResources().getStringArray(R.array.Description); descriptionTextVIEw.setText(description[index]);} /*public voID orderMenu(VIEw vIEw){}*/@OverrIDepublic voID onClick(VIEw v) { switch (v.getID()) { case R.ID.commander: //args.putString("price", textVIEwPrice.getText().toString()); //FragmentManager manager=getFragmentManager(); //MyDialog dialog=new MyDialog(); //dialog.setArguments(args); //dialog.show(manager, "MyDialog"); Bundle args=new Bundle(); args.putString("pricetotal", totalPrice.getText().toString()); //haredPreferences preferences=this.getActivity().getSharedPreferences("Order_Price", Context.MODE_PRIVATE); //haredPreferences.Editor editor=preferences.edit(); //ditor.putString("price1"+textVIEwPrice.toString(), String.valueOf(R.string.Default_prices)); //ditor.commit(); //imageVIEw.setimageResource(ImageAdapter.ThumbsIDs[0]); //ChangeData(0); break; case R.ID.removeMenu : break; case R.ID.addMenu : total=Integer.parseInt(totalPrice.getText().toString())+price; totalPrice.setText(String.valueOf(total)); Toast.makeText(getActivity(), "dish added", Toast.LENGTH_SHORT).show(); break; }}@OverrIDepublic voID onVIEwCreated(VIEw vIEw, Bundle savedInstanceState) { // Todo auto-generated method stub super.onVIEwCreated(vIEw, savedInstanceState); Log.i("bundlebundlebundle", getArguments().getString("price_"+"pizza",Default));}@OverrIDepublic voID onTabReselected(Tab tab, FragmentTransaction ft) { // Todo auto-generated method stub}@OverrIDepublic voID onTabSelected(Tab tab, FragmentTransaction ft) { // Todo auto-generated method stub gallery.setAdapter(new ImageAdapter(getActivity(), tab.getposition())); switch (tab.getposition()) { case 0: imageVIEw.setimageResource(ImageAdapter.ThumbsIDs[0]); break; case 1: imageVIEw.setimageResource(ImageAdapter.ThumbsIDs2[0]); break; case 2: imageVIEw.setimageResource(ImageAdapter.ThumbsIDs3[0]); break; default: imageVIEw.setimageResource(ImageAdapter.ThumbsIDs[0]); break; }}@OverrIDepublic voID onTabUnselected(Tab tab, FragmentTransaction ft) { // Todo auto-generated method stub}
}
这是我使用适配器处理导航抽屉的主要活动(请参阅switch(switch(possition){:
包com.tutecentral.navigationdrawer;
import java.util.ArrayList;import java.util.List;import com.tutecentral.navigationdrawer.R.menu;import androID.os.Bundle;import androID.annotation.Suppresslint;import androID.app.Actionbar;import androID.app.Activity;import androID.app.Fragment;import androID.app.FragmentManager;import androID.content.res.Configuration;import androID.support.v4.app.ActionbarDrawerToggle;import androID.support.v4.vIEw.GravityCompat;import androID.support.v4.Widget.DrawerLayout;import androID.vIEw.Menu;import androID.vIEw.MenuItem;import androID.vIEw.VIEw;import androID.Widget.AdapterVIEw;import androID.Widget.ListVIEw;public class MainActivity extends Activity { private DrawerLayout mDrawerLayout; private ListVIEw mDrawerList; private ActionbarDrawerToggle mDrawerToggle; private CharSequence mDrawerTitle; private CharSequence mTitle; CustomDrawerAdapter adapter; List<DrawerItem> dataList; @Suppresslint("ResourceAscolor")@OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); // Initializing dataList = new ArrayList<DrawerItem>(); mTitle = mDrawerTitle = getTitle(); mDrawerLayout = (DrawerLayout) findVIEwByID(R.ID.drawer_layout); mDrawerList = (ListVIEw) findVIEwByID(R.ID.left_drawer); mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); // Add Drawer Item to dataList dataList.add(new DrawerItem(true)); // adding a spinner to the List dataList.add(new DrawerItem("Main Options"));// adding a header to the List dataList.add(new DrawerItem("Search", R.drawable.ic_action_search)); dataList.add(new DrawerItem("Menu", R.drawable.menu)); dataList.add(new DrawerItem("EnvoIE Facture", R.drawable.bill)); dataList.add(new DrawerItem("Affectations", R.drawable.table)); dataList.add(new DrawerItem("Other Options")); // adding a header to the List dataList.add(new DrawerItem("About", R.drawable.ic_action_about)); dataList.add(new DrawerItem("Settings", R.drawable.setting)); dataList.add(new DrawerItem("Help", R.drawable.ic_action_help)); dataList.add(new DrawerItem("Social")); // adding a header to the List dataList.add(new DrawerItem("Message", R.drawable.ic_action_email)); dataList.add(new DrawerItem("likes", R.drawable.ic_action_good)); dataList.add(new DrawerItem("Twitter", R.drawable.twitter)); adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item,dataList); mDrawerList.setAdapter(adapter); mDrawerList.setBackgroundcolor(R.color.background_NavAda); mDrawerList.setonItemClickListener(new DrawerItemClickListener()); getActionbar().setdisplayHomeAsUpEnabled(true); getActionbar().setHomebuttonEnabled(true); mDrawerToggle = new ActionbarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open,R.string.drawer_close) { public voID onDrawerClosed(VIEw vIEw) { getActionbar().setTitle(mTitle); invalIDateOptionsMenu(); // creates call to onPrepareOptionsMenu() } public voID onDrawerOpened(VIEw drawerVIEw) { getActionbar().setTitle(mDrawerTitle); invalIDateOptionsMenu(); // creates call to onPrepareOptionsMenu() } }; mDrawerLayout.setDrawerListener(mDrawerToggle); if (savedInstanceState == null) { if (dataList.get(0).isspinner() & dataList.get(1).getTitle() != null) { SelectItem(2); } else if (dataList.get(0).getTitle() != null) { SelectItem(1); } else { SelectItem(0); } } } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public voID SelectItem(int possition) { Fragment fragment = null; Bundle args = new Bundle(); switch (possition) { case 2: fragment = new FragmentThree(); args.putString(FragmentThree.ITEM_name, dataList.get(possition).getItemname()); args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList.get(possition).getimgResID()); break; case 3: fragment = new com.tutecentral.navigationdrawer.Menu(); break; case 4: fragment = new bill_send(); //args.putString(FragmentTwo.ITEM_name, dataList.get(possition).getItemname()); //args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(possition).getimgResID()); break; case 5: fragment = new tables(); //args.putString(FragmentThree.ITEM_name, dataList.get(possition).getItemname()); //args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList.get(possition).getimgResID()); break; case 7: fragment = new about(); //args.putString(FragmentTwo.ITEM_name, dataList.get(possition).getItemname()); //args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(possition).getimgResID()); break; case 8: fragment = new setting(); //args.putString(FragmentThree.ITEM_name, dataList.get(possition).getItemname()); //args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList.get(possition).getimgResID()); break; case 9: fragment = new FragmentOne(); args.putString(FragmentOne.ITEM_name, dataList.get(possition).getItemname()); args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition).getimgResID()); break; case 10: fragment = new FragmentTwo(); args.putString(FragmentTwo.ITEM_name, dataList.get(possition).getItemname()); args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(possition).getimgResID()); break; case 11: fragment = new FragmentThree(); args.putString(FragmentThree.ITEM_name, dataList.get(possition).getItemname()); args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList.get(possition).getimgResID()); break; case 12: fragment = new FragmentOne(); args.putString(FragmentOne.ITEM_name, dataList.get(possition).getItemname()); args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition).getimgResID()); break; case 13: fragment = new FragmentThree(); args.putString(FragmentThree.ITEM_name, dataList.get(possition).getItemname()); args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList.get(possition).getimgResID()); break; default: break; } if(com.tutecentral.navigationdrawer.Menu.actionbar!=null){ if (com.tutecentral.navigationdrawer.Menu.actionbar.isShowing()){ com.tutecentral.navigationdrawer.Menu.actionbar.removeAllTabs(); } } fragment.setArguments(args); FragmentManager frgManager = getFragmentManager(); frgManager.beginTransaction().replace(R.ID.content_frame, fragment).commit(); mDrawerList.setItemChecked(possition, true); setTitle(dataList.get(possition).getItemname()); mDrawerLayout.closeDrawer(mDrawerList); } @OverrIDe public voID setTitle(CharSequence Title) { mTitle = Title; getActionbar().setTitle(mTitle); } @OverrIDe protected voID onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle.syncState(); } @OverrIDe public voID onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Pass any configuration change to the drawer toggles mDrawerToggle.onConfigurationChanged(newConfig); } @OverrIDe public boolean onoptionsItemSelected(MenuItem item) { // The action bar home/up action should open or close the drawer. // ActionbarDrawerToggle will take care of this. if (mDrawerToggle.onoptionsItemSelected(item)) { return true; } return false; } private class DrawerItemClickListener implements ListVIEw.OnItemClickListener { @OverrIDe public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw, int position,long ID) { if (dataList.get(position).getTitle() == null) { SelectItem(position); } }
}
}
谢谢.
解决方法:
当您在NavigationDrawer内部单击时,可以使用setArguments()方法通过Bundle声明数据,如下所示:
// int ID = 1;MyFragment newFragment = new MyFragment();Bundle args = new Bundle();args.putInt("ARG_DATA_ID", ID);newFragment.setArguments(args);// replace and commit with fragmenttransaction
然后,在您的Fragment内,使用如下所示的getArgument()方法:
Bundle arg = getArguments();int ID_pass = arg.getInt("ARG_DATA_ID");// ID_pass = 1;
但是,也许您可以在该主题中找到一些有用的提示:Communicating with Other Fragments.
更新:
好吧,我试图从您的代码中举一个例子,但是,这很卑鄙..很难;).
>您不会收到参数,因为在SelectItem方法中,您不会发送带有Menu片段的任何参数(但是您的开关是正确的方式,您的方法是正确的).当您在设置中不发送按钮或任何东西的情况下单击buttonValIDatePrices时,尝试分别尝试发送一个参数,但在另一个捆绑中.刚创建,一个值存储在其中,什么也没有..它与FragmentTransaction没有关系.
>“内部菜单”中,您尝试通过以下方式接收:
Log.i("bundlebundlebundle", getArguments().getString("price_"+"pizza",Default));
但我认为使用以下方法会更好:
Log.i("bundlebundlebundle", getArguments().getString("price_pizza"));
你应该:
首先(在MainActivity中)=将单击的微调器的值从MainActivity获取到您的设置Fragment,创建一个Bundle并将此值设置为SelectItem方法中的fragment,如下所示:
switch(...) { case 2: frag = new Menu(); // get the value of the spinner selected from here! Something like this: String text = ((Spinner) findVIEwByID(R.ID.spinnerSetting)). getSelectedItem().toString(); // String named "spinner_position", this is how you can get it after args.putString("spinner_position", text); break; ...}frag.setArguments(args);// replace the fragment
Second(在Menu中)=接收此参数,如下所示:
// receive using the name "spinner_position", that's it. nothing else.String text_spinner = getArguments().getString("spinner_position");
总结 以上是内存溢出为你收集整理的android-如何在导航抽屉中的两个片段之间通信数据全部内容,希望文章能够帮你解决android-如何在导航抽屉中的两个片段之间通信数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)