java - 将数组列表传递给 fragment ?

标签 java android arrays android-fragments

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

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

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

感谢简单的解释! 提前致谢。

最佳答案

首先通过实现Parcelable接口(interface)让你PieEntry Parcelable

要在第一个 fragment 中写入的代码

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

关于java - 将数组列表传递给 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41453557/

相关文章:

android - 如何从Play商店检索应用程序详细信息

android - 使用 Robolectric 和 AppCompat 运行 Gradle 测试导致 NullPointerException

ios - 将一张图像分成多个部分后,如何在 Swift 中使用 UIImage 数组?

java - 如何将 JVM 参数传递到 MacOS 中通过 java webstart 启动的应用程序?

java - java中如何将字符串从一个数组转移到另一个数组?

java - 在单个客户端上执行两个 HTTPS POST 调用 - Java 到 Vb.Net

C++ 排除非整数用户输入

java - 在java中将json字符串解析为scala案例类

android - 无法安装 Xamarin.Android.Support.v4

jQuery inArray 给出错误的结果