Android 将数据从一个片段传递到另一个片段
我知道,这个问题已经存在于这个站点和其他站点中,但我仍然试图找到一个解决方案,因为到目前为止,每个解决方案都失败了 我有一个活动,它有两个片段。在第一个片段中,我有5个EditText,这个输入我需要传递给第二个片段,但是。。。我在这个包中还有一个Java类,它必须处理这个输入的值,结果必须显示在第二个片段中。 这是一个普通的计算器 这是我的第一个片段:Android 将数据从一个片段传递到另一个片段,android,android-fragments,android-activity,Android,Android Fragments,Android Activity,我知道,这个问题已经存在于这个站点和其他站点中,但我仍然试图找到一个解决方案,因为到目前为止,每个解决方案都失败了 我有一个活动,它有两个片段。在第一个片段中,我有5个EditText,这个输入我需要传递给第二个片段,但是。。。我在这个包中还有一个Java类,它必须处理这个输入的值,结果必须显示在第二个片段中。 这是一个普通的计算器 这是我的第一个片段: package com.example.afotel; import android.os.Bundle; import android.
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;
}
}
这是我的第二个片段:
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;
}
}
如果代码包含文本视图upFive…等。部分,如果我单击“计算”按钮,程序将崩溃。我不知道问题出在哪里,因为我做了这段视频所说的一切:
我知道,这个例子中的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) {
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();
ModelClass modelClass = new ModelClass();
modelClass.setFive(gradeFiveSt );
modelClass.setFour(gradeFourSt );
modelClass.setThree( gradeThreeSt );
modelClass.setTwo(gradeTwoSt );
modelClass.setOnes(gradeOneeSt );
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.gyorsSzamlalas, new jegyekFelLe(modelClass));
fr.commit();
}
});
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 {
ModelClass modelClass;
public jegyekFelLe(ModelClass modelClass) {
this.modelClass = modelClass;
}
@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(modelClass.getFive());
TextView felNegyes = (TextView) rootView.findViewById(R.id.);
upFour.setText(modelClass.getFour());
TextView upThree= (TextView) rootView.findViewById(R.id.upThree);
upThree.setText(modelClass.getThree());
TextView upTwo = (TextView) rootView.findViewById(R.id.upTwo);
upTwo.setText(modelClass.getTwo());
TextView downOne = (TextView) rootView.findViewById(R.id.dowbOne);
leEgyes.setText(modelClass.getOnes());
return rootView;
}
}
public class ModelClass {
private String Ones;
private String Two;
private String Three;
private String Four;
private String Five;
public String getOnes() {
return Ones;
}
public void setOnes(String ones) {
Ones = ones;
}
public String getTwo() {
return Two;
}
public void setTwo(String two) {
Two = two;
}
public String getThree() {
return Three;
}
public void setThree(String three) {
Three = three;
}
public String getFour() {
return Four;
}
public void setFour(String four) {
Four = four;
}
public String getFive() {
return Five;
}
public void setFive(String five) {
Five = five;
}
}
在提交事务之前,设置绑定并向片段添加参数。您可能需要将代码更改为此结构:
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();
您没有将包作为片段中的参数传递 *将数据从片段A发送到片段B
Bundle bundle= new Bundle();
bundle.putString("Your_key",data)
//add items to the bundle then begin transacting
Fragment fragment= new jegyekFelLe();
fragment.setArguments(bundle);
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.gyorsSzamlalas,fragment );
fr.commit();
*从片段a获取数据
String data = getArguments.getString("Your_key")
一,。您需要使用活动的支持片段管理器,而不是片段的子片段管理器 二,。在事务中将该包添加到片段管理器之前,需要将该包作为参数包提供给片段
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
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();
FragmentTransaction fr = (((FragmentActivity)getActivity()).getSupportFragmentManager()).beginTransaction();
Fragment fragment = new jegyekFelLe();
Bundle bundle = new Bundle();
bundle.putString("Five", gradeFiveSt );
bundle.putString("Four", gradeFourSt );
bundle.putString("Three", gradeThreeSt );
bundle.putString("Two", gradeTwoSt );
bundle.putString("Ones", gradeOneeSt );
fragment.setArguments(bundle);
fr.replace(R.id.gyorsSzamlalas, fragment);
fr.commit();
}
在你的Gyorszam碎片中你根本没有使用捆绑包。你也可以使用你的代码,只是在创建捆绑包之后进行碎片调用。谢谢!谢谢你的帮助,现在我明白了!非常感谢您的时间,它的帮助!