Ich arbeite an einer Matrix Calculator App und habe Probleme herauszufinden, wie man einen Wert von jedem EditText in der GridView erhält. Ich muss die Werte erhalten, um sie in eine andere Matrix zu bringen und sie zu berechnen.Wie erhält man den Wert jedes EditText von einem Adapter aus GridView?
So sieht das GridView aus, wenn der Benutzer eine 3x2-Matrix verwenden möchte. Es ist ein Gridview sechs EditTexts weil 3 * 2 = 6
Hier ist das XML für das Bild oben enthält:
<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>
Hier ist die XML für die Gridview-Elemente. Ich weiß nicht, wie man die Werte von ihnen zu erhalten:
<EditText
android:id="@+id/editText3"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="center"
android:ems="10"
android:inputType="numberDecimal" />
Hier ist der MatrixAdapter für das Gridview ist:
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;
}
}
Und die Matrix-Klasse:
public class Matrix {
public int i;
public int j;
public Matrix(int i, int j)
{
this.i = i;
this.j = j;
}
}
Und schließlich, Hier ist der Java-Code, wo ich die Werte von jedem EditText in GridView bekommen werde, aber ich weiß nicht, wie man das macht:
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?
}
}
}
Wenn ich das verwenden, das bedeutet, dass ich einen Wert von EditText unter Verwendung der getView Methode erhalten kann? –
In diesem Code erhalten Sie den Wert von editiertext in onClick-Methode von viewHolder. –