我有一个填充了edittexts的网格视图。当用户按下按钮时,我想要检索存储在edittexts中的所有值,但我只回到第一个。
网格视图位于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
答案 1 :(得分:0)
getview方法存在问题...... 因为你使用布局充气器来扩充editText来获取字符串 但是你需要知道哪个editText包含数据
转到for循环语句以获取所有字符串。
运行for循环作为视图中使用的editText的数量。 for loop
(如果是edittext.gettext()。length()&gt; 0) String data = edittext.gettext();
答案 2 :(得分:0)
我通过在编辑文本上使用文本观察器并从那里更新数组来修复它