個々のビューとして editTexts を含むグリッドビューがあります。別のフラグメントに切り替えたり、ホームボタンを押したりすると消えてしまうため、各 editTexts にある値を保存したいのですが、どうすればよいですか?
すべてのビューを保持する配列または配列リストが必要ですか? 私がそれをした場合、どうすればビューを取得できますか?
fragment_score_red.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RedScorerFragment"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/gridLabel__red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:text="@string/grid_label__red"
android:background="@color/white"
android:paddingTop="5dp"
/>
<GridView
android:id="@+id/gridViewRed"
android:layout_width="wrap_content"
android:layout_height="350dp"
android:layout_alignParentLeft="true"
android:layout_below="@id/gridLabel__red"
android:columnWidth="40dp"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="7dp"
android:gravity="center"
android:background="@color/white"
>
</GridView>
</RelativeLayout>
</ScrollView>
RedScorerFragment.java
import com.actionbarsherlock.app.SherlockFragment;
import android.content.Context;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
public class RedScorerFragment extends SherlockFragment {
LayoutInflater infl;
GridView mGrid;
EditText mText;
int R1C1, R1C2, R1C3;
View tView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
infl = inflater;
//super.onCreate(savedInstanceState);
View mView = inflater.inflate(R.layout.fragment_score_red, container, false);
mGrid = (GridView) mView.findViewById(R.id.gridViewRed);
tView = container.getRootView();
final InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mView.getApplicationWindowToken(), 0);
/*mGrid.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_MOVE)
return true;
return false;
}
});*/
if(savedInstanceState != null)
{
R1C1 = savedInstanceState.getInt("R1C1");
R1C2 = savedInstanceState.getInt("R1C2");
R1C3 = savedInstanceState.getInt("R1C3");
Log.d("savedd R1C1 ", String.valueOf(R1C1));
Log.d("savedd R1C2 ", String.valueOf(R1C2));
Log.d("savedd R1C3 ", String.valueOf(R1C3));
}
mGrid.setAdapter(new ImageAdapter(getActivity()));
return mView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
if(savedInstanceState != null)
{
R1C1 = savedInstanceState.getInt("R1C1", 0);
R1C2 = savedInstanceState.getInt("R1C2", 0);
R1C3 = savedInstanceState.getInt("R1C3", 0);
Log.v("saved R1C1 ", String.valueOf(R1C1));
Log.v("saved R1C2 ", String.valueOf(R1C2));
Log.v("saved R1C3 ", String.valueOf(R1C3));
}
//Log.v("saved R1C1 ", String.valueOf(R1C1));
//Log.v("saved R1C2 ", String.valueOf(R1C2));
//Log.v("saved R1C3 ", String.valueOf(R1C3));
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
//for(int i=0;i<mGrid.getChildCount(); i++)
Log.v("Saving", String.valueOf(mGrid.getAdapter().getItem(0)));
//Log.v("Saving", mGrid.getItemAtPosition(2).toString());
Integer temp = (Integer) mGrid.getAdapter().getItem(0);
int temp2;
if(temp != null)
{
temp2 = temp;
Log.v("temp2", String.valueOf(temp2));
savedInstanceState.putInt("R1C1", temp2);
}
else
Log.v("temp2", "Temp was null");
mText = (EditText)tView.findViewById(R.id.weightedRed__red);
Log.d("weighted Red", mText.getText().toString() );
EditText oText = (EditText)tView.findViewById(R.id.grid_item_edit_text_red);
Log.d("red edit text", oText.getText().toString() );
EditText lText = (EditText)tView.findViewById(R.id.grid_item_edit_text_blue);
Log.d("blue edit text", lText.getText().toString() );
//savedInstanceState.putInt("R1C2", (Integer) mGrid.getAdapter().getItem(4) );
//savedInstanceState.putInt("R1C3", (Integer) mGrid.getAdapter().getItem(2) );
}
public class ImageAdapter extends BaseAdapter{
Context context;
public ImageAdapter(Context context)
{
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View myView = convertView;
//LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null)
{
LayoutInflater li = infl;
myView= li.inflate(R.layout.grid_items, null);
EditText editText = (EditText) myView.findViewById(R.id.grid_item_edit_text_red);
}
return myView;
}
@Override
public int getCount() {
return 9;//only 9
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
}
get Item メソッドをに変更してみました
@Override
public Object getItem(int position) {
return mGrid.getItemAtPosition(position);
}
しかし、ホームボタンを押すとクラッシュし、スタックオーバーフローエラーが発生します