2

I'm working on a Matrix Calculator app and I'm having trouble figuring out how to get a value of each EditText that are in the GridView. I need to get the values in order to put them in another matrix and calculate it.

Here is what the GridView looks like if the user wanted to use a 3x2 matrix. It's a GridView containing six EditTexts because 3*2=6

enter image description here

Here is the XML for that picture above:

<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>

Here is the XML for the GridView items. I don't know how to get the values from them:

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

Here is the MatrixAdapter for the GridView:

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;
    }
} 

And the Matrix class:

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

And finally, here is the Java code where I am going to get the values from each EditText in GridView, but I don't know how to do that:

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?
        }
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Starchipper
  • 167
  • 3
  • 11

2 Answers2

1

I hope this code will help you,

@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;

}

private class 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();

    }
  }); 
}

}

Sandeep Sankla
  • 1,250
  • 12
  • 21
0

You can create an Arraylist to store the values and take wherever you want. To get the values of the edit-texts and store in arraylist for each item use "TextChangeListener()" with your edit-text.

Check this link:

How to use the TextWatcher class in Android?

Zohaib Hassan
  • 984
  • 2
  • 7
  • 11