我有一个gridview,其中包含editTexts作为其中的各个视图。我想保存每个editTexts中的值,因为当我切换到另一个片段或按下主页按钮时它们会消失,所以我该怎么做?
我需要一个包含所有视图的数组或arraylist吗?如果我这样做,我将如何获得意见?
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);
}
但现在当我按下主页按钮并且出现堆栈溢出错误时它会崩溃
答案 0 :(得分:4)
如果这是绝对的和合格的解决方案,我不是一般的,但你可以尝试为你的适配器添加一个所有TextView
的值容器(如数据集)(它可能是一个普通的ArrayList
或类似的事情。)
然后在每个TextWatcher
上设置EditText
:
mEditText.addTextChangedListener(new TextWatcher() {...});
每次更改EditText
值时,都会更改列表项的值。
在onSaveInstanceState()
方法中,您只需要从适配器中检索值。
其余我认为很简单。
编辑(只是为了清楚)
你的适配器应该运气相似:
public class MyAdapter extends BaseAdapter {
// All the fancy stuff
List<String> dataset;
@Override
public View getView(...) {
//... othe code
mEditText.addTextChangedListener(new MyTextWatcher(position));
}
private class MyTextWatcher implements TextWatcher {
int index;
public MyTextWatcher(int index) {
this.index = index;
}
@Override
public void afterTextChanged(Editable s) {
dataset.set(index, s.toString());
}
}
}
答案 1 :(得分:2)
除了TextWatchers
之外,你可以简单地将所有Editexts添加到一个数组中,然后当你需要它们的文本时,你可以简单地遍历数组并在所有数组上调用getText().toString();
它们。
public class ImageAdapter extends BaseAdapter{
Context context;
EditText[] etCollection = new EditText[getCount()];
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);
//^ moved to outside the null block
etCollection[position] = editText;
return myView;
}