我有一个Drills类和几个扩展它的类,例如AddDrill。
我希望能够在活动之间传递此Drill对象“系列”,所以我正在使用Parcelable。
我找到了this答案和this示例,并创建了Drill和AddDrill类,我认为我做错了什么,在我的实现中一切都很好吗?
钻头:
package com.simplemathgame;
import android.os.Parcel;
import android.os.Parcelable;
public abstract class Drill implements Parcelable{
protected int firstNumber;
protected int secondNumber;
protected int answer;
protected int userAnswer;
protected String userAnswerString;
protected String symbol;
protected int minBound, maxBound;
protected int drillNumber;
public String getUserAnswerString() {
return userAnswerString;
}
public void setUserAnswerString(String userAnswerString) {
this.userAnswerString = userAnswerString;
}
Drill(int min, int max){
minBound = min;
maxBound = max;
answer = answerCalculatetion();
userAnswerString = "";
}
public Drill(Parcel in) {
readFromParcel(in);
}
public int getDrillNumber() {
return drillNumber;
}
public void setDrillNumber(int drillNumber) {
this.drillNumber = drillNumber;
}
public int getUserAnswer() {
return userAnswer;
}
public void setUserAnswer(int userAnswer) {
this.userAnswer = userAnswer;
}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public int getMinBound() {
return minBound;
}
public void setMinBound(int minBound) {
this.minBound = minBound;
}
public int getMaxBound() {
return maxBound;
}
public void setMaxBound(int maxBound) {
this.maxBound = maxBound;
}
//getters and setters
public int getFirstNumber() {
return firstNumber;
}
public void setFirstNumber(int firstNumber) {
this.firstNumber = firstNumber;
}
public int getSecondNumber() {
return secondNumber;
}
public void setSecondNumber(int secondNumber) {
this.secondNumber = secondNumber;
}
public int getAnswer() {
return answer;
}
public void setAnswer(int answer) {
this.answer = answer;
}
protected int getRandomNumber(int high, int low){
return (int) ((Math.random( ) * (high - low + 1)) + low);
}
protected abstract int answerCalculatetion();
protected String printDrill(){
String FirstNumberS;
String SecondNumberS;
if(this.getFirstNumber() < 0){
FirstNumberS = "(" + this.getFirstNumber() + ")";
}
else{
FirstNumberS = "" + this.getFirstNumber();
}
if(this.getSecondNumber() < 0){
SecondNumberS = "(" + this.getSecondNumber() + ")";
}
else{
SecondNumberS = "" + this.getSecondNumber();
}
return (FirstNumberS + " " + this.getSymbol() + " " + SecondNumberS + " " + "=");
}
protected String printDrillAnswer(){
String FirstNumberS;
String SecondNumberS;
String answerS;
if(this.getAnswer() < 0){
answerS = "(" + this.getAnswer() + ")";
}
else{
answerS = "" + this.getAnswer();
}
if(this.getFirstNumber() < 0){
FirstNumberS = "(" + this.getFirstNumber() + ")";
}
else{
FirstNumberS = "" + this.getFirstNumber();
}
if(this.getSecondNumber() < 0){
SecondNumberS = "(" + this.getSecondNumber() + ")";
}
else{
SecondNumberS = "" + this.getSecondNumber();
}
return (FirstNumberS + " " + this.getSymbol() + " " + SecondNumberS + " " + "=" + " " + answerS);
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// We just need to write each field into the
// parcel. When we read from parcel, they
// will come back in the same order
dest.writeInt(firstNumber);
dest.writeInt(secondNumber);
dest.writeInt(answer);
dest.writeInt(userAnswer);
dest.writeString(userAnswerString);
dest.writeString(symbol);
dest.writeInt(minBound);
dest.writeInt(maxBound);
dest.writeInt(drillNumber);
}
/**
*
* Called from the constructor to create this
* object from a parcel.
*
* @param in parcel from which to re-create object
*/
protected void readFromParcel(Parcel in) {
// We just need to read back each
// field in the order that it was
// written to the parcel
firstNumber = in.readInt();
secondNumber = in.readInt();
answer = in.readInt();
userAnswer = in.readInt();
userAnswerString = in.readString();
symbol = in.readString();
minBound = in.readInt();
maxBound = in.readInt();
}
}
AddDrill:
package com.simplemathgame;
import android.os.Parcel;
import android.os.Parcelable;
public class AddDrill extends Drill {
AddDrill(int min, int max) {
super(min, max);
symbol = "+";
firstNumber = getRandomNumber(min, max);
secondNumber = getRandomNumber(min, max);
answer = answerCalculatetion();
}
private AddDrill(Parcel in) {
super(in);
}
protected int answerCalculatetion() {
return (this.getFirstNumber() + this.getSecondNumber());
}
public static final Parcelable.Creator<AddDrill> CREATOR = new Parcelable.Creator<AddDrill>() {
public AddDrill createFromParcel(Parcel in) {
return new AddDrill(in);
}
public AddDrill[] newArray(int size) {
return new AddDrill[size];
}
};
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
}
谢谢
最佳答案
在您的AddDrill类中,您将需要实现writeToParcel()
方法以及Parcel
的构造函数。您可能还需要对这两种方法都使用@Override
,但是您需要尝试找出最终的解决方案
关于android - Android Parcelable和继承,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14046667/