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

卡米拉·科西斯

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

我有一个活动,它有 2 个片段。在第一个片段中,我有 5 个 EditText,这个输入我需要传递给第二个片段,但是......我在这个包中还有一个 Java 类,它必须使用这个输入的值,而这个结果我必须显示在第二个片段中。这是一个平均计算器。

这是我的第一个片段:

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...(etc.) 部分,如果我点击计算按钮,程序就会崩溃。我不知道有什么问题,因为我做了这个视频所说的一切:https : //www.youtube.com/watch?v=_4i5Jk5RnFA

我知道,这个例子中的 Java Class 没有被使用,那是因为我的代码有这个问题。首先,我想知道如何在两个 Fragment 之间传递数据,然后在我编写之后从这 5 个 EditText 中计算出我需要的结果。我是 Android Studio 的初学者,所以我需要简单、难以理解的解决方案。

我希望你能理解我的问题,对不起我的英语。

卡洛斯·阿尼纳

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

    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();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Bundle()将值从1个片段传递到另一个片段?

如何从一个片段到另一个片段获取捆绑数据?

无法通过navController的setGraph中的包将数据从一个片段传递到另一个片段

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

如何将数据从一个片段传递到上一个片段?

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

将string.xml中的值从一个片段传递到另一活动?

将数据从一个组件传递到另一个Vuejs

将数据从一个ViewModel传递到另一个Android MVVM

无法将数据传递到Android(Kotlin)中的另一个片段

使用Android中的ViewModel将数据与带有recyclerview的片段传递到另一个片段

从一个片段到另一个片段的交易问题

如果一次可见两个片段,如何将数据从一个片段传递到另一个片段

如何将数据从一个片段传递到xml中定义的子片段?

将JSON数据从一个片段传递到另一个片段的最佳方法是什么

将数据从一个片段传递到另一个片段时无法正常工作

将片段状态/数据从一个片段保存到另一个片段

如何将HashSet对象从一个片段传递到android中的另一个片段

将数据从一个碎片传递到另一个碎片函数

如何将数据从片段传递到另一个-Android

无法从一个片段接收数据到另一个片段

将长值从一个片段通过主活动传递到另一个片段

无法将字符串值从一个片段传递到另一个片段

将数据从一个组件传递到另一个组件

将数据从片段传递到另一个片段。未附加到上下文的片段

React Native - 将数据从一个组件传递到另一个

将 Spinner 值从一个片段传递到另一个片段

尝试显示从一个片段到另一个片段的传递对象的数据时出现 NullPointerException

将一个片段重定向到另一个片段