我有一个充满编辑文本的网格视图。当用户按下按钮时,我想检索存储在编辑文本中的所有值,但我只取回第一个值。
网格视图位于 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);
}
}
}