0

我有一個帶有editTexts的gridview作爲其中的單個視圖。我想保存每個editTexts中的值,因爲當我切換到另一個片段或按Home按鈕時它們消失,那麼我該怎麼做?如何從網格視圖獲取值

我是否需要保存所有的意見數組或數組列表?如果我這樣做,我會如何得到意見?

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; 
     } 


    } 


} 

我試圖改變獲取項方法

  @Override 
     public Object getItem(int position) { 

      return mGrid.getItemAtPosition(position); 
     } 

,但現在它崩潰時,我按下home鍵和我得到一個堆棧溢出錯誤

+0

默認情況下'EditText's'有值嗎? –

+0

沒有我輸入他們的東西,然後當我關閉它,它存儲了輸入 – rasen58

回答

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; 
    } 
+0

謝謝保持視圖位置的特拉克... – rasen58

+0

沒有問題。很高興它有幫助。 – mango

4

我不是舒爾如果這是絕對和eligant的解決方案,但你可以嘗試添加到您的適配器值容器(如數據集)的所有TextView的(也可能是一個普通的ArrayList或類似的財產以後)。

然後在每個EditText設置TextWatcher

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()); 
    } 

} 
} 
+0

值我怎麼會得到所有的'編輯Texts'的,雖然添加了'文字watchers'? – rasen58

+0

我這樣看問題:你添加一個觀察者,但使用其他答案的方式 –

+0

謝謝,我做到了!它的工作原理 – rasen58

相關問題