如何从GridView的适配器获取每个EditText的值?

时间:2017-05-26 20:41:47

标签: java android gridview

我正在使用Matrix Calculator应用程序,而且我无法弄清楚如何获取GridView中每个EditText的值。我需要获取值,以便将它们放在另一个矩阵中并进行计算。

如果用户想要使用3x2矩阵,这就是GridView的样子。它是一个包含六个EditTexts的GridView,因为3 * 2 = 6

enter image description here

以上是上图中的XML:

<RelativeLayout
    android:layout_width="368dp"
    android:layout_height="495dp"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp">

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"
        android:text="Please input the numbers on both matrices." />

    <TextView
        android:id="@+id/textView11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView10"
        android:layout_alignStart="@+id/textView10"
        android:layout_below="@+id/textView10"
        android:layout_marginTop="21dp"
        android:text="Matrix1: "
        android:textStyle="bold" />

    <GridView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_alignLeft="@+id/textView11"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignStart="@+id/textView11"
        android:layout_below="@+id/textView11" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Please fill in all of the fields"
        android:textColor="#FF0000"
        android:visibility="invisible"
        android:layout_below="@+id/grid"
        android:layout_alignLeft="@+id/grid"
        android:layout_alignStart="@+id/grid"
        android:layout_marginTop="15dp" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView8"
        android:layout_alignStart="@+id/textView8"
        android:layout_below="@+id/textView8"
        android:layout_marginTop="11dp"
        android:onClick="setEmptyToZero"
        android:text="Set Empy Fields to Zero" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button4"
        android:layout_marginLeft="28dp"
        android:layout_marginStart="28dp"
        android:layout_toEndOf="@+id/button4"
        android:layout_toRightOf="@+id/button4"
        android:onClick="nextMatrix"
        android:text="Next Matrix" />

</RelativeLayout>

这是GridView项目的XML。我不知道如何从中获取价值:

<EditText
    android:id="@+id/editText3"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:ems="10"
    android:inputType="numberDecimal" />

以下是GridView的MatrixAdapter:

public class MatrixAdapter extends BaseAdapter {
    Context context;
    List<Matrix> matrixList;

    //Or create an int[rows][columns] CHANGE INTO THAT LATER
    public MatrixAdapter(Context context, List<Matrix> matrixList) {
        this.context = context;
        this.matrixList = matrixList;
    }

    @Override
    public int getCount() {

        return matrixList.size();
    }

    @Override
    public Object getItem(int i) {

        return matrixList.get(i);
    }

    @Override
    public long getItemId(int i) {

        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v=View.inflate(context,R.layout.grid_item,null);
        return v;
    }
} 

Matrix类:

public class Matrix {
    public int i;
    public int j;
    public Matrix(int i, int j)
    {
        this.i = i;
        this.j = j;
    }
}

最后,这里是Java代码,我将从GridView中的每个EditText获取值,但我不知道如何做到这一点:

private MatrixAdapter adapter;
private List<Matrix> matrixList;
private int rows;
private int columns;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_subtract_matrix_input);

    // GET THE MATRIX DIMENSIONS FOR BOTH MATRICES
    Bundle extras = getIntent().getExtras();
    rows = extras.getInt("rows");
    columns = extras.getInt("columns");

    //MATRIX 1 INPUT

    // INITIALISE THE GRID
    GridView grid=(GridView)findViewById(R.id.grid);
    grid.setNumColumns(columns);

    // CREATE A LIST OF MATRIX OBJECTS
    //Or create an EditText[rows][columns] CHANGE INTO THAT LATER
    //Matrix[][] matrix1 = new Matrix[rows][columns];
    matrixList = new ArrayList<>(); //THIS IS ONLY FOR THE MATRIX OF EDIT_TEXTS
    //Getting the matrix values will come from the user input

    // ADD SOME CONTENTS TO EACH ITEM
    for (int i=0;i<rows;i++)
    {
        for (int j=0;j<columns;j++)
        {
            matrixList.add(new Matrix(i,j));
        }
    }

    // CREATE AN ADAPTER  (MATRIX ADAPTER)
    adapter = new MatrixAdapter(getApplicationContext(),matrixList);

    // ATTACH THE ADAPTER TO GRID
    grid.setAdapter(adapter);

}

public void nextMatrix(View view) //The button goes to the next matrix
{
    //It collects what the user inputted too
    //Checks if one of the fields are empty
    //But how to get the values from each EditText?

}

public void setEmptyToZero(View view) //Fills the empty text boxes to zero
{
    //how to make the program add zeros to the text fields?
    //iterate through the textField matrix first. If an empty field is
    //found, add zero to it

    EditText textField;
    boolean fieldIsEmpty;
    for (int i=0;i<rows;i++)
    {
        for (int j=0;j<columns;j++)
        {
            //How to get values from the EditText Matrix?
        }
    }
}

2 个答案:

答案 0 :(得分:1)

我希望这段代码可以帮到你,

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

if (convertView == null) {
    convertView = LayoutInflater.from(context).inflate(R.layout.R.layout.grid_item, parent, false);
    viewHolder = new ViewHolder(convertView);
    convertView.setTag(viewHolder);
} else {
    viewHolder = (ViewHolder) convertView.getTag();
}

Item currentItem = (Item) getItem(position);
viewHolder.itemName.setText(currentItem.getItemName());  // if already in your  list:

return convertView;

}

私有类ViewHolder {     EditText itemName;

public ViewHolder(View view) {
    itemName = (EditText)view.findViewById(R.id.editText3);
    itemname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
           String text = itemName..getText().toString();

    }
  }); 
}

}

答案 1 :(得分:0)

您可以创建一个Arraylist来存储值并随身携带。要获取编辑文本的值并在每个项目的arraylist中存储,请使用带有编辑文本的“TextChangeListener()”。

检查此链接:

How to use the TextWatcher class in Android?