2013-01-07 69 views
0

我有一個網格視圖充滿edittexts。當用戶按下按鈕時,我想要檢索存儲在edittext中的所有值,但我只是返回第一個值。只有第一個值在android網格視圖正在返回

網格視圖位於fragment_score.xml中。

網格視圖grid_items.xml

<EditText 
    android:id="@+id/grid_item_edit_text_red" 
    android:inputType="number" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 


    /> 

<EditText 
    android:id="@+id/grid_item_edit_text_blue" 
    android:inputType="number" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 

    /> 

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 


import com.actionbarsherlock.app.SherlockFragment; 
import com.actionbarsherlock.view.MenuInflater; 
import com.actionbarsherlock.view.Menu; 

import android.content.Context; 
import android.os.Bundle; 
import android.app.Activity; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
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 RedFragment extends SherlockFragment { 


LayoutInflater infl; 
GridView mGrid; 

String[] etCollection = new String[9]; 


ImageAdapter adapter; 

View mView; 


Context mContext; 
Button calc;  

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    System.out.println("onCreateView called"); 
    infl = inflater; 
    //super.onCreate(savedInstanceState); 
    super.onCreateView(inflater, container, savedInstanceState); 
    mView = inflater.inflate(R.layout.fragment_score, container, false); 
    mGrid = (GridView) mView.findViewById(R.id.gridViewRed); 

    setHasOptionsMenu(true); 

    //tView = container.getRootView(); 
    mContext = mView.getContext(); 

    for(int j=0;j<9;j++) 
    { 
     etCollection[j] = "-1"; 
    } 




    adapter = new ImageAdapter(mContext); 
    mGrid.setAdapter(adapter); 
    Log.e("debug", "here2"); 
    calc = (Button)mView.findViewById(R.id.calc_red); 

    calc.setOnTouchListener(new OnTouchListener(){ 
     @Override 
     public boolean onTouch(View v, MotionEvent event){ 
      calculate(); 
      return true; 

     } 
    }); 


    return mView; 
} 







public void getGridVals(){ 

    Log.v("vals", "Getting grid Vals()"); 

    for(int i=0;i<9;i++) 
    { 
     EditText tempText1 = (EditText)mView.findViewById(R.id.grid_item_edit_text_red); 
     Log.v("temp text ", "." + tempText1.getText().toString()); 
        //this should update etCollection[] 
     adapter.getView(i, mView, null); 
      //this always prints the value stored in the first textview 
     Log.d("etCollection", etCollection[i]); 



    } 



} 

public void setGridVals(int i, String val){ 

    etCollection[i] = val; 


} 


public void calculate(){ 
    int[][] pegs = new int[3][3]; 
    getGridVals(); 
} 



public class ImageAdapter extends BaseAdapter{ 

    Context context; 




    public ImageAdapter(Context context) 
    { 
     this.context = context; 
     etCollection = new String[getCount()]; 

    } 


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     View myView = convertView; 


     if(convertView == null) 
     { 
      LayoutInflater li = infl; 
      myView= li.inflate(R.layout.grid_items, null); 


     } 
      /////////////////////////////// 
     //this seems to always return the value in the first R.id.grid_item_edit_text_red 
     EditText editText = (EditText) myView.findViewById(R.id.grid_item_edit_text_red); 
     etCollection[position] = editText.getText().toString(); 

     return myView; 
    } 

    @Override 
    public int getCount() { 

     return 9; 
    } 



    @Override 
    public Object getItem(int position) { 
     Log.w("etCollection: " + position , etCollection[position]); 
     return etCollection[position]; 
    } 

    @Override 
    public long getItemId(int position) { 

     return 0;//mGrid.getItemIdAtPosition(position); 
    } 


} 

}

回答

0

我用在編輯文本的文本觀察家和更新從那裏

0

編輯的文本,我還是有點一款Android新手,但我認爲你應該爲每個EditText都有一個id,而你只有定義2.
所以當你做(EditText)mView.findViewById(R.id.grid_item_edit_text_red)它會每次都返回相同的EditText。
這可能有點幫助:http://developer.android.com/guide/topics/ui/layout/gridview.html

+0

陣列實際上是一個gridview固定它,你不需要,但我不能得到正確的價值觀 – rasen58

0

有一個在getview方法的問題... 爲u已經使用的佈局充氣膨脹的EDITTEXT得到串 但你要知道,這是EDITTEXT包含數據

轉至for循環語句以獲取所有字符串。

運行for循環 鑑於使用EDITTEXT的數目.. for循環

(如果edittext.gettext()長度()> 0。) 字符串數據= edittext.gettext();

+0

我試過了,但它不起作用 – rasen58

相關問題