单击按钮将数据从Fragment移动到新Activity

时间:2014-11-10 18:16:27

标签: java android

我在Viewpager中有三个Framgments,我想通过单击按钮从其中一个开始一个新的Activity。我还想从四个Spinner元素中获取一些信息。我能够从Spinners获取信息并保存在全局变量中,但是当我单击按钮时,变量变为null。我无法解决这个问题,这让我很生气!
以下是我如何制作片段并找到正确的布局XML:

public class SlideFragment extends Fragment {

    private int currentPageNumber;
    private View mainView;

    public static SlideFragment create(int pageNumber, String origin) {
        SlideFragment myFragment = new SlideFragment();

        Bundle args = new Bundle();
        args.putInt("page", pageNumber);
        args.putString("origin", origin);
        myFragment.setArguments(args);

        return myFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        currentPageNumber = getArguments().getInt("page");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        String origin = getArguments().getString("origin");
        // This is the mainActivity
        Rastimar rastimar = new Rastimar();

        if (origin == "profile") {
            if (currentPageNumber == 0) {
                mainView = inflater.inflate(R.layout.profile_screen1, container, false);
            }
            else {
                mainView = inflater.inflate(R.layout.profile_screen2, container, false);
            }
        }
        else if (origin == "rastimi") {
            switch (currentPageNumber) {
                case 0:
                    mainView = inflater.inflate(R.layout.rastimar, container, false);
                    rastimar.makeSpinners(mainView);
                    rastimar.makeListView(mainView);
                    break;
                case 1:
                    mainView = inflater.inflate(R.layout.rastima_leit, container, false);
                    rastimar.makeSpinnersForRastimaLeit(mainView);
                    break;
                case 2:
                    mainView = inflater.inflate(R.layout.rastima_yfirlit, container, false);
                    rastimar.makeExpandableListView(mainView);
                    rastimar.makeSpinnersForYfirlit(mainView);
                    break;
            }
        }

        return mainView;
    }
}

这里我从微调器获取数据,这是有效的。

private class onSpinnerSelected implements AdapterView.OnItemSelectedListener {
    public void onItemSelected(AdapterView<?> parent, View view,
                               int pos, long id) {
        // Find the spinner that was pressed by id
        switch(parent.getId()) {
            case R.id.dates:
                selectedRastimaDate = parent.getItemAtPosition(pos).toString();
                break;
            case R.id.courses:
                selectedCourse = parent.getItemAtPosition(pos).toString();
                break;
            case R.id.spinner_start_time:
                selectedStartTime = parent.getItemAtPosition(pos).toString();
                break;
            case R.id.spinner_end_time:
                selectedEndTime = parent.getItemAtPosition(pos).toString();
                break;
            case R.id.spinner_dates:
                selectedDate = parent.getItemAtPosition(pos).toString();
                break;
        }
    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }
}

这是按下按钮时调用的方法,putExtra方法中的所有变量都突然为null:

public void findAvailableTime(View view) {
    Intent lausir_timar = new Intent(this, LausirTimar.class);
    lausir_timar.putExtra("Date", selectedRastimaDate);
    lausir_timar.putExtra("Course", selectedCourse);
    lausir_timar.putExtra("StartTime", selectedStartTime);
    lausir_timar.putExtra("EndTime", selectedEndTime);
    startActivity(lausir_timar);
}

这是Fragment的XML布局文件

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="cityboys.golfapp.Rastimar">

    <TextView
        android:id="@+id/rastimaLeit_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:textStyle="bold"
        android:textColor="#2d7165"
        android:layout_centerHorizontal="true"
        android:text="Rástímaleit"
        android:paddingBottom="10dp"/>

    <Spinner
        android:id="@+id/dates"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rastimaLeit_title"
        android:textColor="#2d7165"
        android:layout_marginRight="5dp"
        android:layout_alignParentLeft="true"/>

    <Spinner
        android:id="@+id/spinner_start_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rastimaLeit_title"
        android:textColor="#2d7165"
        android:layout_marginRight="5dp"
        android:layout_alignParentRight="true"/>

    <Spinner
        android:id="@+id/courses"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dates"
        android:textColor="#2d7165"
        android:layout_marginRight="5dp"
        android:layout_alignParentLeft="true"/>

    <Spinner
        android:id="@+id/spinner_end_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/spinner_start_time"
        android:textColor="#2d7165"
        android:layout_marginRight="5dp"
        android:layout_alignParentRight="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Lausir tímar"
        android:id="@+id/button"
        android:layout_below="@+id/spinner_end_time"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:onClick="findAvailableTime" />

</RelativeLayout>

<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="#2d7165"
    android:dividerHeight="0.5dp"
    android:background="#2b574e" />

我不确定我应该提供哪些更多信息,如果这还不够,只需要更多信息! 提前谢谢。

0 个答案:

没有答案