1

My problem is, that i am trying to pass final List<PieEntry> entries = new ArrayList<>(); to another fragment, which is more or less empty until now.

My complete code is this: ( of the fragment which initiates the 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;
    }
}

Simple explanations are appreciated! Thanks in advance.

1 Answer 1

1

First of all make the make you PieEntry Parcelable by implementing interface Parcelable

Code to writeen in first Fragemnt

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

data is a list of PieEntry

Code to written in second Fragemnt

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            data  =getArguments().getParcelableArrayList("data");
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

How do I implement the interface Parcelable?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.