通过刷卡视图选项卡机器人工作室,没有按钮将数据传递给另一个片段视图、机器人、选项卡、片段

2023-09-06 08:32:38 作者:淡妆素眸

是否有可能通过刷卡从片段的数据传递给片段?

Is it possible to pass a data from fragment to fragment by swipe?

有很多文章教我们如何从片段片段传递数据,但大多数文章或问题的在他们的第一个片段,其中用于传递价值的另一个片段实现OnClickListener。

There are many articles teaching us how to pass the data from fragment to fragment, but most of the article or questions had implemented OnClickListener in their first fragment, which used to pass value to another fragment.

不过我的情况是通过从两个片段中的数据没有任何按钮,点击并最终将它们保存在不同的表中通过单击最后一个片段按钮。我能做些什么来实现这一目标?

But my case is pass the data from two fragments without any button click and finally save them in different tables by clicking button in the last fragment. What can I do to achieve this??

该流程信息 >> 劳动力 >> WorkDetailsTable 键,将它们保存到不同的桌子点击一个按钮。

The flow is Information >> WorkForce >>WorkDetailsTable and save them to different table by one button click.

我试图去解决它,但我得到的SQLite NULL值。我想我已经错过了很多,但也没办法。请帮我...我一直停留在这里两天多...谢谢

I have tried to work it out but I get NULL value in SQLite. I think I have miss out a lot but have no idea. PLEASE help me...I've been stuck at here for more than two days...Thanks

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, ViewGroup container, Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            View work = inflater.inflate(R.layout.workforce, container, 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, ViewGroup container, Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     View workDetails = inflater.inflate(R.layout.tableworkdetails, container, 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);

        }

注意:我的情况是通过从两个片段中的数据没有任何按钮,点击并最终将它们保存在不同的表中通过单击最后一个片段按钮

推荐答案

如果我正确理解你的问题,你基本上实现像一个精灵,其中每个步骤并将信息传递到下一个步骤,你刷卡之间的一点点选项​​卡或选择它们。

If I understand your problem correctly, you are essentially implementing something a little bit like a "Wizard" where each step passes information to the next step as you swipe between the tabs or select them.

因此​​,在现实的问题是如何选择的时候才能出的信息片段,当它被取消选中,成为一个片段。

So in reality your problem is how to get the information out of a fragment when it is deselected and into a fragment when selected.

最简单地说,我建议你的活动包含所有的信息的主人的副本,进入/它需要从每个片段的标签寻呼机适配器通过它。

At the simplest level I would suggest your activity holds the "master" copy of all of the information and passes it into/takes it from each fragment in your tab pager adapter.

您将需要某种形式的域的对象来容纳所有你需要收集的信息。每个选项卡将只更新的信息,它关心的位。

You would need some kind of "Domain" object to hold all the information you need to collect. Each tab would only update the bits of information it cares about..

public class WorkData {
 string information;
 string subCon;
... etc..
}

您添加一个这样的实例来保存主副本到你的标签活动:

You add an instance of this to hold the master copy to your "tab" activity:

public class Tab extends ActionBarActivity implements ActionBar.TabListener {
...
 WorkData workData = new WorkData();
...

我会再提出一个简单的界面,你的每一个标签片段实​​施;是这样的:

I would then suggest a simple interface that each of your "tab" fragments implement; something like:

public interface DataUpdate {
 void setData(WorkData data);
 WorkData getData();
}

每个标签的片段会实现这个接口,根据需要更新WorkData ..

Each of your tab fragments would implement this interface, updating the WorkData as required..

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).
}

然后你只需要添加code来传递数据来回......在你的标签活动,看起来像......

Then you just need to add the code to pass the data backwards and forwards.. in your "Tab" activity which 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);
}

这里要注意一个重要的事情是,你的TabPagerAdapter将每次调用getItem()时创建一个新的片段..这将意味着我们将永远不会得到任何更新,因为每次我们设法得到它返回一个新片段空片段。我们需要改变这一点,以便碎片还在创建时的第一要求,但只创建一次,这样我们就不能保持扔掉我们的工作。

An important thing to notice here is that your TabPagerAdapter will create a new fragment every time you call getItem().. that will mean that we will never get any updates because each time we try to get the fragment it returns a new, empty fragment. We need to change this so that the fragments are still created when first asked for, but only created once so that we don't keep throwing away 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;
        }

希望这有助于。祝你好运: - )

Hope this helps. Good luck :-)