任何人都可以帮我制作 arraylist final ArrayList<PieEntry> entries = new ArrayList<>();
在我的代码中可以分配,因此我可以在片段 a 和 b
- 将代码分段:
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 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);
final ArrayList<PieEntry> entries = new ArrayList<>();
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));
editText3.setText("");
editText2.setText("");
editText3.requestFocus();
}
}
});
return view;
}
}
片段b代码:
public class FirstFragment extends Fragment {
private String title;
private int page;
public static FirstFragment newInstance(int page, String title) {
FirstFragment fragmentFirst = new FirstFragment();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("someTitle", title);
fragmentFirst.setArguments(args);
return fragmentFirst;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
page = getArguments().getInt("someInt", 0);
title = getArguments().getString("someTitle");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
EditText editText = (EditText) view.findViewById(R.id.editText);
final PieChart piechart = (PieChart) view.findViewById(R.id.piechart);
final Button button2 = (Button) view.findViewById(R.id.button2);
new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int position) {
}
};
button2.setOnClickListener(new View.OnClickListener() {
public void onClick (View view) {
getResources().getColor(R.color.violp);
getResources().getColor(R.color.bluep);
getResources().getColor(R.color.redp);
getResources().getColor(R.color.greenp);
getResources().getColor(R.color.yellowp);
getResources().getColor(R.color.orangep);
getResources().getColor(R.color.lightbluep);
getResources().getColor(R.color.purplep);
getResources().getColor(R.color.darkredp);
PieDataSet set = new PieDataSet(entries, "");
set.setColors(new int[]{R.color.bluep, R.color.greenp, R.color.violp, R.color.redp, R.color.yellowp, R.color.orangep, R.color.lightbluep, R.color.purplep, R.color.darkredp}, getActivity());
PieData datax = new PieData(set);
piechart.setData(datax);
Toast.makeText(getActivity(), (R.string.loading),
Toast.LENGTH_SHORT).show();
piechart.setNoDataText(String.valueOf(R.string.nodata));
piechart.notifyDataSetChanged();
piechart.animateX(2000, Easing.EasingOption.EaseInExpo);
piechart.animateY(2000, Easing.EasingOption.EaseInExpo);
}
});
return view;
}
到目前为止,我已经尝试了很多选项,但我无法让程序运行。 任何帮助都是经过认证的。 的谢谢!
答案 0 :(得分:1)
为了传输数据,我们需要对其进行包裹或序列化。 Android操作系统默认情况下将Parcelable提升为序列化。序列化是一个java概念。在您的情况下,您需要创建&#34; PieEntry&#34;作为Parcelable的类。为此,您需要在模型中实现Parcelable接口。
检查以下实施情况。
package com.cludprinting.mysample;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by nithinjith.pp on 05-01-2017.
* Implement Parcelable in PieEntry.
*
*/
public class PieEntry implements Parcelable {
public static final Parcelable.Creator<PieEntry> CREATOR = new Parcelable.Creator<PieEntry>() {
@Override
public PieEntry createFromParcel(Parcel source) {
return new PieEntry(source);
}
@Override
public PieEntry[] newArray(int size) {
return new PieEntry[size];
}
};
// These are sample instance variables
// Add your own instance variable
private String mName;
private String mDOB;
private int mAge;
public PieEntry(Parcel parcel) {
this.mName = parcel.readString();
this.mDOB = parcel.readString();
this.mAge = parcel.readInt();
}
public int getmAge() {
return mAge;
}
public void setmAge(int mAge) {
this.mAge = mAge;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.mName);
dest.writeString(this.mDOB);
dest.writeInt(this.mAge);
}
public String getmDOB() {
return mDOB;
}
public void setmDOB(String mDOB) {
this.mDOB = mDOB;
}
public String getmName() {
return mName;
}
public void setmName(String mName) {
this.mName = mName;
}
}
实例变量和访问方法可能会根据您的实现而改变。