Android 将整数数组从一个片段传递到另一个片段
我是Android应用程序开发的业余爱好者。Android 将整数数组从一个片段传递到另一个片段,android,Android,我是Android应用程序开发的业余爱好者。 我试着找了很多,但没有得到满意的答案 谁能告诉我该怎么做 这是我的主要活动课 public class MainActivity extends AppCompatActivity implements TopSection.TopSectionListener{ static int arr[]; TextView textView; @Override protected void onCreate(Bundle savedInstanceSt
我试着找了很多,但没有得到满意的答案 谁能告诉我该怎么做 这是我的主要活动课
public class MainActivity extends AppCompatActivity implements
TopSection.TopSectionListener{
static int arr[];
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void sorting(int[] random) {
arr=random;
BottomSection bottomSection=(BottomSection) getFragmentManager().findFragmentById(R.id.fragment4);
bottomSection.setArray(arr);
}
}
这是我的第一个片段的代码
public class TopSection extends Fragment {
private static TextView top;
private static Button gen;
private static Button sort;
static int random[];
String s;
TopSectionListener activityCommander;
public interface TopSectionListener{
public void sorting(int random[]);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try{
activityCommander=(TopSectionListener)context;
}catch (ClassCastException e){
throw new ClassCastException(context.toString());
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.top_section_fragment, container, false);
top=(TextView)view.findViewById(R.id.top);
gen=(Button) view.findViewById(R.id.gen);
sort=(Button) view.findViewById(R.id.sort);
random=new int[20];
for (int i=0;i<20;i++){
random[i]=0;
}
s="";
top.setText(s);
gen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
butClk(v,s);
}
});
sort.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickBut(v);
}
});
return view;
}
public void butClk(View view,String s){
for (int i=0;i<20;i++) {
Random rand = new Random();
random[i] = rand.nextInt(89) + 10;
s+=random[i]+" ";
}
top.setText(s);
}
public void clickBut(View view){
activityCommander.sorting(random);
}
}
ArrayList<Integer> integerArray = new ArrayList<Integer>();
getFragmentManager().beginTransaction().replace(R.id.yourContainer,SecondFragment.newInstance(integerArray)).commit();
公共类TopSection扩展片段{
私有静态文本视图顶部;
专用静态按钮发生器;
私有静态按钮排序;
静态int随机[];
字符串s;
活动指挥器;
公共接口侦听器{
公共空白排序(int random[]);
}
@凌驾
公共void-onAttach(上下文){
super.onAttach(上下文);
试一试{
activityCommander=(TopSectionListener)上下文;
}catch(ClassCastException e){
抛出新的ClassCastException(context.toString());
}
}
@可空
@凌驾
创建视图时的公共视图(LayoutFlater充气机、@Nullable ViewGroup容器、Bundle savedInstanceState){
视图=充气机。充气(R.layout.top\u section\u碎片,容器,假);
top=(TextView)view.findViewById(R.id.top);
gen=(按钮)view.findViewById(R.id.gen);
sort=(按钮)view.findViewById(R.id.sort);
随机=新整数[20];
对于(int i=0;i您可以通过使用接口来实现。下面是一个完整的答案,并附有示例,只需从String
更改为int[]
您可以通过使用接口来完成。下面是一个完整的答案和示例,只需从String
更改为int[]
您不能在片段之间传递整数数组。但您可以传递整数数组列表。请将整数数组转换为Arraylist,并在第一个片段中按如下方式传递它
public class TopSection extends Fragment {
private static TextView top;
private static Button gen;
private static Button sort;
static int random[];
String s;
TopSectionListener activityCommander;
public interface TopSectionListener{
public void sorting(int random[]);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try{
activityCommander=(TopSectionListener)context;
}catch (ClassCastException e){
throw new ClassCastException(context.toString());
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.top_section_fragment, container, false);
top=(TextView)view.findViewById(R.id.top);
gen=(Button) view.findViewById(R.id.gen);
sort=(Button) view.findViewById(R.id.sort);
random=new int[20];
for (int i=0;i<20;i++){
random[i]=0;
}
s="";
top.setText(s);
gen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
butClk(v,s);
}
});
sort.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickBut(v);
}
});
return view;
}
public void butClk(View view,String s){
for (int i=0;i<20;i++) {
Random rand = new Random();
random[i] = rand.nextInt(89) + 10;
s+=random[i]+" ";
}
top.setText(s);
}
public void clickBut(View view){
activityCommander.sorting(random);
}
}
ArrayList<Integer> integerArray = new ArrayList<Integer>();
getFragmentManager().beginTransaction().replace(R.id.yourContainer,SecondFragment.newInstance(integerArray)).commit();
ArrayList integerArray=new ArrayList();
getFragmentManager().beginTransaction().replace(R.id.yourContainer,SecondFragment.newInstance(integerArray)).commit();
将这些代码添加到第二个片段中以获得数组
public static SecondFragment newInstance(ArrayList<Integer> intValues) {
SecondFragment fragment = new SecondFragment();
Bundle args = new Bundle();
args.putIntegerArrayList(ARG_PARAM1, intValues);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
ArrayList<Integer> intValue = getArguments().getIntegerArrayList(ARG_PARAM1);
}
}
publicstaticsecondfragmentnewinstance(arraylistintvalues){
SecondFragment=新的SecondFragment();
Bundle args=新Bundle();
args.putIntegerArrayList(ARG_PARAM1,intValues);
fragment.setArguments(args);
返回片段;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
如果(getArguments()!=null){
ArrayList intValue=getArguments().getIntegerArrayList(ARG_PARAM1);
}
}
您不能在片段之间传递整数数组。但您可以传递整数数组列表。请将整数数组转换为Arraylist,并在第一个片段中按如下方式传递它
public class TopSection extends Fragment {
private static TextView top;
private static Button gen;
private static Button sort;
static int random[];
String s;
TopSectionListener activityCommander;
public interface TopSectionListener{
public void sorting(int random[]);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try{
activityCommander=(TopSectionListener)context;
}catch (ClassCastException e){
throw new ClassCastException(context.toString());
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.top_section_fragment, container, false);
top=(TextView)view.findViewById(R.id.top);
gen=(Button) view.findViewById(R.id.gen);
sort=(Button) view.findViewById(R.id.sort);
random=new int[20];
for (int i=0;i<20;i++){
random[i]=0;
}
s="";
top.setText(s);
gen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
butClk(v,s);
}
});
sort.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickBut(v);
}
});
return view;
}
public void butClk(View view,String s){
for (int i=0;i<20;i++) {
Random rand = new Random();
random[i] = rand.nextInt(89) + 10;
s+=random[i]+" ";
}
top.setText(s);
}
public void clickBut(View view){
activityCommander.sorting(random);
}
}
ArrayList<Integer> integerArray = new ArrayList<Integer>();
getFragmentManager().beginTransaction().replace(R.id.yourContainer,SecondFragment.newInstance(integerArray)).commit();
ArrayList integerArray=new ArrayList();
getFragmentManager().beginTransaction().replace(R.id.yourContainer,SecondFragment.newInstance(integerArray)).commit();
将这些代码添加到第二个片段中以获得数组
public static SecondFragment newInstance(ArrayList<Integer> intValues) {
SecondFragment fragment = new SecondFragment();
Bundle args = new Bundle();
args.putIntegerArrayList(ARG_PARAM1, intValues);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
ArrayList<Integer> intValue = getArguments().getIntegerArrayList(ARG_PARAM1);
}
}
publicstaticsecondfragmentnewinstance(arraylistintvalues){
SecondFragment=新的SecondFragment();
Bundle args=新Bundle();
args.putIntegerArrayList(ARG_PARAM1,intValues);
fragment.setArguments(args);
返回片段;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
如果(getArguments()!=null){
ArrayList intValue=getArguments().getIntegerArrayList(ARG_PARAM1);
}
}
发布您的代码我们将帮助您更好地发布您的代码我们将帮助您更好BlankFragment fragment=new BlankFragment();出现一些问题。:(表示无法解决BlankFragment这些代码必须添加到您的BottomSection片段中。您添加了吗?完成了!已使用的服务和广播接收器。:)BlankFragment=new BlankFragment();出现了一些问题。:(表示无法解决BlankFragment这些代码必须添加到您的BottomSection片段中。您添加了吗?完成了!已使用的服务和广播接收器。:)