将整数数组从一个片段传递到另一个片段

时间:2017-09-30 17:45:49

标签: android

我是Android应用程序开发的业余爱好者 我尝试了很多搜索,但无法得到满意的答案。

有人可以告诉我应该怎么做吗?

这是我的主要活动课程

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

这是我的第二个片段的代码

    public class BottomSection extends Fragment {

int arr[];
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.bottom_section_fragment, container, false);
    populateQuickListView(view);
    populateMergeListView(view);
    return view;
}
public void setArray(int arr1[]){
    arr=arr1;
}
private void populateQuickListView(View view) {
    String a[]=new String[20];
    for (int i=0;i<20;i++){
        a[i]=""+arr[i];
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),R.layout.item_pop,a);
    ListView list=(ListView)view.findViewById(R.id.quick);
    list.setAdapter(adapter);
}

private void populateMergeListView(View view) {
    String a[]=new String[20];
    for (int i=0;i<20;i++){
       a[i]=""+arr[i];
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),R.layout.item_pop,a);
    ListView list=(ListView)view.findViewById(R.id.merge);
    list.setAdapter(adapter);
}
}

我知道这是一个编写得很糟糕的代码,但我刚刚开始,这件事花了我很多时间。
请帮助。

2 个答案:

答案 0 :(得分:0)

您可以使用界面来完成。以下是一个完整的答案示例,只需从int[]更改为String

how to add tooltips on winform list box items

答案 1 :(得分:0)

您不能将整数数组从片段传递到片段。但是你可以传递整数数组列表。将整数数组转换为Arraylist,并在第一个片段

中按如下方式传递
ArrayList<Integer> integerArray = new ArrayList<Integer>();
        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);

    }
}
相关问题