将数组列表传递给片段?

时间:2017-01-03 22:45:49

标签: java android arrays android-fragments

我的问题是,我正试图通过       final List<PieEntry> entries = new ArrayList<>();到另一个fragment,直到现在或多或少是空的。

我的完整代码是:( fragment启动ArrayList)

package com.example.nextpietwo;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;

import java.util.ArrayList;
import java.util.List;

    public class SecondFragment extends Fragment {
     // Store instance variables
    private String title;
    private int page;

// newInstance constructor for creating fragment with arguments
public static SecondFragment newInstance(int page, String title) {
    SecondFragment fragmentSecond = new SecondFragment();
    Bundle args = new Bundle();
    args.putInt("someInt", page);
    args.putString("someTitle", title);
    fragmentSecond.setArguments(args);
    return fragmentSecond;
}

// Store instance variables based on arguments passed
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    page = getArguments().getInt("someInt", 0);
    title = getArguments().getString("someTitle");
}

// Inflate the view for the fragment based on layout XML
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_second, container, false);
    TextView tvLabel2 = (TextView) view.findViewById(R.id.textView2);

    //Alle Objekte hier einfügen (wie textview tvavel2)
    final List<PieEntry> entries = new ArrayList<>();
    final Button button = (Button) view.findViewById(R.id.button);
    final TextView textView = (TextView) view.findViewById(R.id.textView);
    final TextView textView2 = (TextView) view.findViewById(R.id.textView2);
    final EditText editText = (EditText) view.findViewById(R.id.editText);
    final EditText editText2 = (EditText) view.findViewById(R.id.editText2);
    final EditText editText3 = (EditText) view.findViewById(R.id.editText3);

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick (View view) {

            if (editText3.getText().toString().matches("")) {
                Toast.makeText(getActivity(), "You did not enter a Valid Item ID", Toast.LENGTH_LONG).show();
                return;

            } else if (editText2.getText().toString().matches("")) {
                Toast.makeText(getActivity(), "You did not enter a Valid Quantitiy", Toast.LENGTH_LONG).show();
                return;

            } else {
                String nomen = (editText3.getText().toString());
                float number = Float.parseFloat(editText2.getText().toString());

                entries.add(new PieEntry(number, nomen));

                Bundle args = new Bundle();
                args.putStringArrayList("array", ArrayList);
                FirstFragment.setArguments(args);

                editText3.setText("");
                editText2.setText("");

                editText3.requestFocus();

            }

        }
    });

    return view;
    }
}

简单的解释表示赞赏! 提前致谢。

1 个答案:

答案 0 :(得分:1)

首先通过实现接口PieEntry

让你Parcelable Parcelable

要编写的代码first Fragemnt

SecondFragment secondfragment //where data needs to be pass
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("data", data);
secondfragment.setArguments(bundle);

数据是PieEntry

的列表

second Fragemnt

写的代码
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            data  =getArguments().getParcelableArrayList("data");
        }
    }