0

我有一个充满编辑文本的网格视图。当用户按下按钮时,我想检索存储在编辑文本中的所有值,但我只取回第一个值。

网格视图位于 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);
    }


}

}

4

3 回答 3

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

于 2013-01-07T03:39:17.857 回答
0

我通过在编辑文本上使用文本观察器并从那里更新数组来修复它

于 2013-01-22T05:04:48.350 回答
0

getview 方法中存在问题...因为您使用布局充气器对 editText 进行充气以获取字符串但是您需要知道哪个 editText 包含数据

使用 for 循环语句来获取所有字符串。

运行 for 循环作为视图中使用的 editText 的数量.. for 循环

(如果 edittext.gettext().length()>0) 字符串数据 = edittext.gettext();

于 2013-01-07T05:06:45.720 回答