首先,我要感謝大家在高級時間和幫助。我的問題可能是一個基本而愚蠢的問題。不過,我是初學者,我想這是一個學習的好地方!如何正確實現SparseArray以在ViewPager的每個實例上存儲自定義對象? Android開發 - Java
所以,我有一個名爲Question的自定義對象,它在Question.java中描述;它看起來像這樣:
import android.os.Parcel;
import android.os.Parcelable;
public class Question implements Parcelable{
String mark;
String total;
String worth;
public Question(String amark, String atotal, String aworth) {
mark = amark;
total = atotal;
worth = aworth;
}
public Question(Parcel in) {
mark = in .readString();
total = in .readString();
worth = in .readString();
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
private void readFromParcel(Parcel in) {
mark = in .readString();
total = in .readString();
worth = in .readString();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeString(mark);
dest.writeString(total);
dest.writeString(worth);
}
public static final Parcelable.Creator<Question> CREATOR = new Parcelable.Creator<Question>() {
public Question createFromParcel(Parcel in) {
return new Question(in);
}
public Question[] newArray(int size) {
return new Question[size];
}
};
}
現在我創建一個用戶定義數量的窗體來從用戶收集信息。所以我試圖創建一個SparseArray,其中每個條目都包含一個Question對象(所以我可以將所有這些信息傳遞給另一個活動來實現)。看起來,現在我有了所有的過往工作。然而,因爲每次打開我的UserFragment.java(每個'form'都是UserFragment的一個實例,由FirstFragment.java控制,這是創建視圖頁面的活動),基本上我創建了一個新的SparseList問題,因爲我的UserFragment.java看起來是這樣的:
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class UserFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.layout_avgcalcform,
container, false);
final int pagetotal = getArguments().getInt("size");
TextView tv = (TextView) rootView.findViewById(R.id.avg_testtv);
tv.setText(String.valueOf(getArguments().getInt("page_position")));
String Text;
if(pagetotal==getArguments().getInt("page_position")){
Text = "Submit";
} else
Text = "next";
Button b = (Button) rootView.findViewById(R.id.button1);
b.setText(Text);
b.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//Extracting the information from the user input forms! NOTE:NEED TO ADD ERROR CHECKING
final EditText markc = (EditText)rootView.findViewById(R.id.editTextASSMARK);
final EditText marktotc = (EditText)rootView.findViewById(R.id.editTextASSTOT);
final EditText assvalc = (EditText)rootView.findViewById(R.id.editTextASSWORTH);
//Converting from INFO string to int
double currentassmark = Double.parseDouble(markc.getText().toString());
double currentassworth = Double.parseDouble(marktotc.getText().toString());
double currentassval = Double.parseDouble(assvalc.getText().toString());
// Determining the current viewnumber (or form number) the user is currently on.
int currentviewnum = ((FirstFragment) getActivity()).getPager().getCurrentItem();
saveData(new Question(markc.getText().toString(),marktotc.getText().toString(),assvalc.getText().toString()),currentviewnum);
((FirstFragment) getActivity()).getPager().setCurrentItem(currentviewnum+1);
double totalpercentage =(double)((currentassmark/currentassworth)*currentassval);
Bundle bundle = new Bundle();
int currentpagenumber = getArguments().getInt("page_position");
String currentpage = String.valueOf(currentpagenumber);
//bundle.putDouble(currentpage, totalpercentage);
if(pagetotal==currentpagenumber){
bundle.putString("TEST", currentpage);
Intent intent = new Intent(UserFragment.this.getActivity(), DisplayAverageActivity.class);
bundle.putInt("numberofforms", getArguments().getInt("page_position"));
bundle.putSparseParcelableArray("Questions",questions);
intent.putExtras(bundle);
startActivity(intent);
}
}
});
return rootView;
}
//this is an error, on every creation of UserFragment, it basically creates a new questions array.
// This results in only the the existance of (questions.get(lastpage).hello).
SparseArray<Question> questions = new SparseArray<Question>();
public void saveData(Question quest, int pagenumber){
questions.put(pagenumber, quest);
}
}
我不能想辦法把這個(正確地)做不定義每次叫問題一個新的SparseArray。所以基本上當我嘗試訪問這個,當然,只有不給我一個空指針的迭代是最後一個。當我像這樣訪問它時例如:String markk = questions.get(totalasses-1).mark;只有totalasses-1作品。所以我真的很感激它,如果有人可以幫我解決我的問題,我該如何定義數組,比如FirstFragment.java(或者UserFragment外部的地方),以便它只創建一次SparseArray,然後只是用相應的查看尋呼機表格填充每個條目的問題表單?
如果可以的話,請包括一些編碼示例,以及一些解釋,因爲我是初學者,我也想學習以及尋找解決方案!
非常感謝。
哦,那麼,「靜態」聲明如何解決我的問題?你能解釋一下嗎?而第二個建議的解決方案,這基本上是將它存儲到UserFragment內的savedInstanceState。所以每次我調用它時,我都會提取數組,然後添加,存儲,然後重複,直到?首次創建UserFragment時的第一個實例如何工作?謝謝你的幫助:)我只需要多一點理解! – Leon92
我應該提供一個關於static關鍵字的鏈接,我的錯誤。讓你的變量static對於UserFragment的每個實例基本上都是全局的。所以基本上,它屬於每個UserFragment,數據將在每個實例中保留。 – Steven
如何通過UserFragment的每個實例修復它,如果我總是創建它的一個新實例? – Leon92