将数据从一个片段传递到另一个

将数据从一个片段传递到另一个

问题描述:

我知道,此问题已在此站点和其他站点中存在,但我仍然尝试查找解决方案,因为到目前为止,每个解决方案都失败了.

I known, that this question is already exist in this site and others, but I still try find a solution, because so far every solution is failed.

我有一个活动,其中包含2个片段.在第一个Fragment中,我有5个EditText,此输入我需要传递到第二个Fragment,但是...我在此Package中也有一个Java类,它必须使用此输入的值,结果我必须显示在第二个片段中. 这是一个普通的计算器.

I have an Activity, which has 2 Fragments. In the first Fragment I have 5 EditText, and this inputs I need to pass to the second Fragment, but... I also have a Java Class in this Package, which has to work with this inputs's value, and that result I have to display in the second Fragment. This is a average calculator.

这是我的第一个片段:

package com.example.afotel;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class gyorsSzam extends Fragment {

    EditText gradeFive, gradeFour, gradeThree, gradeTwo, gradeOne;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_gyors_szam, container, false);
        Button calculate = (Button) view.findViewById(R.id.gGyors);

        gradeFive = (EditText) view.findViewById(R.id.numberOfGardeFive);
        gradeFour = (EditText) view.findViewById(R.id.numberOfGardeFour);
        gradeThree = (EditText) view.findViewById(R.id.numberOfGardeThree);
        gradeTwo = (EditText) view.findViewById(R.id.numberOfGardeTwo);
        gradeOne = (EditText) view.findViewById(R.id.numberOfGardeOne);

        calculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction fr = getFragmentManager().beginTransaction();
                fr.replace(R.id.gyorsSzamlalas, new jegyekFelLe());
                fr.commit();

                String gradeFiveSt = gradeFive.getText().toString();
                String gradeFourSt = gradeFour.getText().toString();
                String gradeThreeSt = gradeThree.getText().toString();
                String gradeTwoSt = gradeTwo.getText().toString();
                String gradeOneeSt = gradeOne.getText().toString();

                Bundle bundle = new Bundle();
                bundle.putString("Five", gradeFiveSt );
                bundle.putString("Four", gradeFourSt );
                bundle.putString("Three", gradeThreeSt );
                bundle.putString("Two", gradeTwoSt );
                bundle.putString("Ones", gradeOneeSt );

            }
        });

        return view;
    }
}

这是我的第二个片段:

package com.example.afotel;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

public class jegyekFelLe extends Fragment {


    public jegyekFelLe() {


    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_jegyek_fel_le, container, false);

        Bundle bundle = getArguments();

        String gradeFive = bundle.getString("Five");
        String gradeFour = bundle.getString("Four");
        String gradeThree  = bundle.getString("Three");
        String gradeTwo = bundle.getString("Two");
        String gradeOne = bundle.getString("One");

        TextView upFive = (TextView) rootView.findViewById(R.id.upFive);
        upFive.setText(gradeFive);

        TextView felNegyes = (TextView) rootView.findViewById(R.id.);
        upFour.setText(gradeFour);

        TextView upThree= (TextView) rootView.findViewById(R.id.upThree);
        upThree.setText(gradeThree);

        TextView upTwo = (TextView) rootView.findViewById(R.id.upTwo);
        upTwo.setText(gradeTwo);

        TextView downOne = (TextView) rootView.findViewById(R.id.dowbOne);
        leEgyes.setText(gradeOne);

        return rootView;
    }
}

如果代码包含TextView upFive ...(等)部分,则当我单击计算"按钮时,程序将崩溃.我不知道这是什么问题,因为我做了这部影片所说的一切: https://www.youtube.com/watch?v=_4i5Jk5RnFA

If the code contains the TextView upFive...(etc.) part, the program is crash if i click the calculate button. I don't know what is the problem, because i do everything that this video is say: https://www.youtube.com/watch?v=_4i5Jk5RnFA

我知道,此示例中未使用Java类,这是因为我的代码存在此问题.首先,我想知道如何在两个Fragment之间传递数据,然后编写以计算这5个EditText所需的结果. 我是Android Studio的入门者,因此我需要简单,难以理解的解决方案.

I know, that the Java Class in this example is not used, that's because I have this problem with the code. First I want to know how to pass data between two Fragments, and after I write to calculate the result that I need from this 5 EditText. Im a starter in Android Studio, so i need easy, undarstandable solution.

希望您能理解我的问题,对不起我的英语.

I hope You can understand my problem, sorry for my english.

设置包并在提交事务之前将参数添加到片段.您可能需要将代码更改为以下结构:

Set the bundle and add arguments to the fragment before committing transaction. You may have to change your code to this structure:

    Bundle bundle= new Bundle();
    //add items to the bundle then begin transacting
    Fragment jegyekFelLe= new jegyekFelLe();
    jegyekFelLe.setArguments(bundle);
    FragmentTransaction fr = getFragmentManager().beginTransaction();
    fr.replace(R.id.gyorsSzamlalas,jegyekFelLe );
    fr.commit();