-1

I'm still a novice at Android Studio and I have one more task to do on my first Linear Algebra-related project before I am comfortable with coding the rest of it. The task is to get each value of EditText in a matrix of EditTexts and store them into an int[][] matrix.

Now I'm really stuck on this for hours and the main reason is because I don't know how to probably use GridView and GridLayout. I also don't know which one is better to use for this.

Below is a representation of what I'm trying to do:

Let's say that the user wants to calculate with 2x3 matrices

enter image description here

The app will then display a 2x3 matrix of empty EditTexts:

enter image description here

And the user entered whatever numbers they want in the empty EditTexts enter image description here

When they tap on the "Next Matrix" button, I want to get the numbers from each EditText and put them into an int[][] matrix so that can transfer it to another Activity.

If I can do that, I'll be able to print out the values of each EditText as a TextView.

And again, which Grid is better to use in order to place the matrix of EditTexts? GridView or GridLayout? How do I get the values of each EditTexts from them?

Starchipper
  • 167
  • 3
  • 11

2 Answers2

0

I think your problem on displaying matrix grid was solved. You might need to get the Matrix contents now. Try this

// SETUP YOUR GRIDVIEW
GridView mGridView = (GridView)findViewById(R.id.grid);
// COUNT TOTAL CHILDS IN IT
int gridchilds = mGridView.getChildCount();
// ITERATE THROUGH EACH CHILDS
for(int i = 0; i < gridchilds; i++) 
{
    // SELECT 'i' th CHILD IN A VIEWGROUP
    ViewGroup gridChild = (ViewGroup) mGridView.getChildAt(i);
    // GET THE CHILDREN SIZE OF THAT 'i' th CHILD
    int childSize = gridChild.getChildCount();
    // ITERATE THOUGH EACH
    for(int k = 0; k < childSize; k++) 
    {
        // CHECK IF IT IS AN 'EditText'
        if( gridChild.getChildAt(k) instanceof EditText)
        {
            // GET THE MATRIX VALUE AT THAT CELL
            String matrix_value=gridChild.getChildAt(k)).getText().toString();
        }
    }
}

Hope it helps

Sangeeth Nandakumar
  • 1,362
  • 1
  • 12
  • 23
  • Is the 'i' th child in a ViewGroup a row? In other words, are the childs the rows of the matrix? – Starchipper May 27 '17 at 15:23
  • No. We are getting each child linearly. i th child in a ViewGroup starts from 1st row and ends in last row traversed linearly – Sangeeth Nandakumar May 27 '17 at 15:53
  • Okay I ran into a few problems when debugging this. `String matrix_value=gridChild.getChildAt(k).getText().toString();` is producing an error so I changed it to `EditText element = (EditText) gridChild.getChildAt(k); matrix_value = element.getText().toString();` but when I debug it, it couldn't identify the value of `matrix_value`. Also, since we are iterating each child linearly, I ran the debugger with a 3x3 matrix and I expect the `childSize` to be 3, but it stays as 1. – Starchipper May 27 '17 at 16:21
0

You can achieve it by using RecyclerView with GridLayoutManager.

Try the following - however I'm not sure the last method's logic (in getValues()) is correct. You may need to tweak up there.

(Looks like you're familiar with mathematic more than I do, so I leave it there for you ;))

Hope it'll work

RecyclerView rv = (RecyclerView) findViewById(your_recycler_view_id);
rv.setLayoutManager(new GridLayoutManager(context, COLUMN_COUNT, GridLayoutManager.VERTICAL, false));
rv.setAdapter(new YourCustomAdapter());

class YourCustomAdapter() {
    EditText[][] editTexts = new EditText[COLUMN_COUNT][ROW_COUNT];

    public YourCustomAdapter() {
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(your_edit_text_layout, parent, false);
        return new YourViewHolder(view, listener);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder h, int position) {
        YourViewHolder holder = (YourViewHolder) h;
        editTexts[position / COLUMN_COUNT][position % COLUMN_COUNT] = holder.editText;
    }

    @Override
    public int getItemCount() {
        return COLUMN_COUNT * ROW_COUNT;
    }

    private class YourViewHolder extends RecyclerView.ViewHolder {
        EditText editText;
        private YourViewHolder() {
            super(itemView);
            this.editText = itemView.findViewById(your_edit_text_id);
        }
    }

    public int[][] getValues() {
        int [][] values = new int[ROW_COUNT][COLUMN_COUNT];

        for(int i = 0; i < ROW_COUNT; i ++) {
            for(int j = 0; j < COLUMN_COUNT; j ++) {
                values[i][j] = 
                Integer.parseInt(editTexts[i][j].getText().toString());
            }
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Yuta
  • 286
  • 1
  • 7
  • What do I put for `context` for `rv.setLayoutManager(new GridLayoutManager(context, COLUMN_COUNT, GridLayoutManager.VERTICAL, false)); ? Also what should `YourCustomAdapter()` be a subclass for in order for the `setAdapter()` method to work? – Starchipper May 27 '17 at 16:48
  • First, `context` would be your `Activity` (which extends `Context`) or, `Fragment#getContext()` if you're using a fragment. Second, (sorry I neglected the super class), yourCustomAdapter should extends `RecyclerView.Adapter` – Yuta May 27 '17 at 23:25