Java – passes data to another fragment through the slider view with no buttons
Can I transfer data from clip to clip by sliding?
Many articles teach us how to pass data from fragment to fragment, but most articles or problems have implemented onclicklistener in their first fragment, which passes values to another fragment
But in my case, data is transferred from two segments without any button click. Finally, click the button in the last segment to save them in different tables What can I do to achieve this?
Streams are information > > workforce > > workdetailstable, and save them to different tables by clicking a button
I've tried to use it, but I get null in SQLite I think I miss it, but I don't know Please help me... I'm trapped here for more than two days... Thank you
Tab. java
public class Tab extends ActionBarActivity implements ActionBar.TabListener { ViewPager Tab; TabPagerAdapter TabAdapter; ActionBar actionBar; public static String name = null; public static String subContractors = null; // will be used for data communication public static Force force_bean;; public static Info info_bean; public static Force getForce(){ return force_bean; } public static void setForce(Force force){ force_bean=force; } public static Info getInfo(){ return info_bean; } public static void setInfo(Info info){ info_bean=info; } final Activity mActivity = (Activity) this; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tab1); info_bean = new Info(); force_bean = new Force(); TabAdapter = new TabPagerAdapter(getSupportFragmentManager()); Tab = (ViewPager) findViewById(R.id.pager); Tab.setOnPagechangelistener( new ViewPager.SimpleOnPagechangelistener() { @Override public void onPageSelected(int position) { actionBar = ((AppCompatActivity) mActivity).getSupportActionBar(); actionBar.setSelectedNavigationItem(position); } }); Tab.setAdapter(TabAdapter); actionBar = ((AppCompatActivity) mActivity).getSupportActionBar(); //Enable Tabs on Action Bar actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); //Add New Tabs actionBar.addTab(actionBar.newTab().setText("Information").setTabListener(this)); actionBar.addTab(actionBar.newTab().setText("Work Force").setTabListener(this)); actionBar.addTab(actionBar.newTab().setText("Work Details").setTabListener(this)); } @Override public void onTabSelected(ActionBar.Tab tab,android.support.v4.app.FragmentTransaction ft) { } @Override public void onTabUnselected(ActionBar.Tab tab,android.support.v4.app.FragmentTransaction ft) { } @Override public void onTabReselected(ActionBar.Tab tab,android.support.v4.app.FragmentTransaction ft) { } }
TabPagerAdapter. java
public class TabPagerAdapter extends FragmentStatePagerAdapter { public TabPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { switch (i) { case 0: return Information.newInstance("name"); case 1: return WorkForce.newInstance("SubCon"); case 2: return WorkDetailsTable.newInstance(); } return null ; } @Override public int getCount() { return 3; //No of Tabs you can give your number of tabs }
Informmation. java
public class Information extends Fragment implements View.OnClickListener { private Spinner spinner,spinner2,spinner3; private static String a; public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { super.onCreate(savedInstanceState); View info = inflater.inflate(R.layout.information,container,false); dialog = new DateDialog(); spinner = (Spinner)info.findViewById(R.id.spinner); addItemsOnSpinner(); a= spinner.getSelectedItem().toString(); return info; } public static Information newInstance(String a) { Information fragment=new Information(); Bundle bundle=new Bundle(); bundle.putString("a",a); fragment.setArguments(bundle); return fragment; } public void addItemsOnSpinner() { List<String> list = new ArrayList<String>(); list.add("1 "); list.add("2"); ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_dropdown_item,list); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); }
WorkForce. java
public class WorkForce extends Fragment { private static EditText txt1; private static String subCon; public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) { super.onCreate(savedInstanceState); View work = inflater.inflate(R.layout.workforce,false); txt1 = (EditText) work.findViewById(R.id.editText); subCon = txt1.getText().toString(); return work; } public static WorkForce newInstance(String subCon) { WorkForce f = new WorkForce(); Bundle bundle = new Bundle(); bundle.putString("subCon",subCon); f.setArguments(bundle); return f; } }
WorkDetails. java
private com.example.project.project.API.InfoAPI ts; private com.example.project.project.API.WorkDetailsAPI WD; private com.example.project.project.API.WorkForceAPI WF; public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) { super.onCreate(savedInstanceState); View workDetails = inflater.inflate(R.layout.tableworkdetails,false); getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); spinnerTra = (Spinner) workDetails.findViewById(R.id.spinner6); addItemsOnSpinner(); Button btn1 = (Button)workDetails.findViewById(R.id.button2); WD = new com.example.project.project.API.WorkDetailsAPI(getActivity()); ts = new com.example.project.project.API.InfoAPI(getActivity()); WF = new com.example.project.project.API.WorkForceAPI(getActivity()); a1 = spinnerTra.getSelectedItem().toString(); Bundle bundle = new Bundle(); final String name = bundle.getString("a"); final String subContractors = bundle.getString("subCon"); btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { add(name,subContractors); } }); return workDetails; } public void add(String name,String subContractors) { Toast.makeText(getActivity(),+name+subContractors,Toast.LENGTH_SHORT).show(); ts.insertTimeSheet(name); WF.insertWorkForce(subContractors); }
Note: in my case, I transfer data from two fragments, and finally save them in different tables by clicking the button in the last fragment
Solution
If I understand your problem correctly, you will basically implement something like "Wizard". When you slide or select them between tags, each step will pass the information to the next step
So in reality, your problem is how to take the information from the clip when deselecting and get it from the clip when selecting
At the simplest level, I recommend that your activity save a "master" copy of all information and pass it to / from each segment in your tab pager adapter
You will need some kind of "domain" object to hold all the information you need to collect Each tag will only update its concerned information bits
public class WorkData { string information; string subCon; ... etc.. }
You can add an instance to save the master copy to the tab activity:
public class Tab extends ActionBarActivity implements ActionBar.TabListener { ... WorkData workData = new WorkData(); ...
Then I will suggest a simple interface for each "tag" fragment implementation; It's like:
public interface DataUpdate { void setData(WorkData data); WorkData getData(); }
Each of your tag fragments will implement this interface and update workdata as needed
public class WorkForce extends Fragment implements DataUpdate { ... private WorkData workData; // this fragment's "copy" of the data ... @Override public WorkData getData() { this.workData.subCon = this.subCon; // Assuming subcon has been updated.. else use txt1.getText(); return this.workData; } @Override public void setData(WorkData workData) { this.workData = workData; // Update this page's views with the workData... // This assumes the fragment has already been created and txt1 is set to a view txt1.setText(workData.subCon); this.subCon = workData.subCon; // Actually Could just use subCon in workData,but be aware that workData actually points to the Activity's copy (kinda makes getdata redundant.. but I like symmetry and Couldn't be bothered making lots of copies of the object). }
Then you just need to add code to pass data forward and backward Looks like
@Override public void onTabSelected(ActionBar.Tab tab,android.support.v4.app.FragmentTransaction ft) { int position = tab.getPosition(); DataUpdate dataUpdate = (DataUpdate) TabAdapter.getItem(position); // Pass the master workdata to the selected fragment dataUpdate.setData(this.workData); } @Override public void onTabUnselected(ActionBar.Tab tab,android.support.v4.app.FragmentTransaction ft) { int position = tab.getPosition(); DataUpdate dataUpdate = (DataUpdate) TabAdapter.getItem(position); // Update the master workdata from the unselected fragment this.workData = dataUpdate.getData(); } @Override public void onTabReselected(ActionBar.Tab tab,android.support.v4.app.FragmentTransaction ft) { // This might be pointless,but we'll do it anyway.. int position = tab.getPosition(); DataUpdate dataUpdate = (DataUpdate) TabAdapter.getItem(position); // Pass the master workdata to the selected fragment dataUpdate.setData(this.workData); }
An important thing to note here is that your tabpageradapter will call getitem () every time Create a new fragment, which means we will never get any updates, because it returns a new empty fragment every time we try to get the fragment We need to change this so that the fragment is still created when it is first requested, but only once, so that we won't continue to abandon our work
public class TabPagerAdapter extends FragmentStatePagerAdapter { private static final int NUMBER_OF_TABS = 3; private Fragment[] tabList = new Fragment[NUMBER_OF_TABS]; public TabPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { if (tabList[i] != null) { // Return a tab we created earlier.. return tabList[i]; } else { switch (i) { case 0: tabList[0] = Information.newInstance("name"); return tabList[0]; case 1: tabList[1] = WorkForce.newInstance("SubCon"); return tabList[1]; case 2: tabList[2] = WorkDetailsTable.newInstance(); return tabList[2]; } } return null ; } @Override public int getCount() { return NUMBER_OF_TABS; }
I hope this will help Good luck: -)