如何使用滑动视图传递数据

时间:2015-10-04 16:32:52

标签: java android android-intent swipe

我创建了滑动视图在android中有两个标签栏。我想知道是否可以将数据从第一个选项卡传递到第二个选项卡,最后通过一次button点击将所有数据保存在SQLite中?我知道可以使用按钮传递数据但是通过使用滑动视图听起来可能吗?谢谢:))

Information.java

       public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            spinner = (Spinner)info.findViewById(R.id.spinner);
            return info;
        }

 public void addItemsOnSpinner() {
        List<String> list = new ArrayList<String>();
        list.add("1");
        list.add("2");
        list.add("3");
        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);


    }

WorkDetails.java

    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            View work=inflater.inflate(R.layout.workforce,container,false);
            button = (Button)work.findViewById(R.id.button7);
            txt1 = (EditText)work.findViewById(R.id.editText);
            Button btn1=(Button)getView().findViewById(R.id.button2);
            btn1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                         // save data
       }
            });

            return workDetails;
        }

被修改

在完成一些文档后,我尝试构建一个简单的项目。但是我得到了这个错误:

 Process: com.example.project.project, PID: 2068
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
            at com.example.project.project.Information.onCreateView(Information.java:46)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1026)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)

以下是我的代码段

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 spinner = (Spinner)info.findViewById(R.id.spinner);
        a= spinner.getSelectedItem().toString();
  Fragment fragment=new Fragment();
        Bundle bundle=new Bundle();
        bundle.putString("a",a);
 fragment.setArguments(bundle);
        return info;
    }

 public void addItemsOnSpinner() {
            List<String> list = new ArrayList<String>();
            list.add("1");
            list.add("2");
            list.add("3");
            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);


        }

WorkDetails.java

  Bundle bundle=this.getArguments();
       final String name=bundle.getString("a");
  Button btn1=(Button)getView().findViewById(R.id.button2);
        btn1.setOnClickListener(new View.OnClickListener() {
           public void onClick(View arg0) {
               W1=txtWork1.getText().toString();
                a1 = spinnerTra.getSelectedItem().toString();
                P1=per1.getText().toString();
                ts.insertTimeSheet(name);
                WD.insertWorkDetails(a1,W1,P1);

            }
        });

        return workDetails;
    }

1 个答案:

答案 0 :(得分:0)

这已被问过很多次了。这是一个标准问题,已在android文档中解决。请仔细阅读http://developer.android.com/training/basics/fragments/communicating.html部分以便更好地理解。