0

I have successfully programmatically created a TableLayout having multiple TextViews and EditTexts's inside.

What I want: is to getText from the EditTexts when text has been entered.

My attempt: is to use addTextChangedListener() in the loop where I create the TableLayout.

My problem is: I managed to get the text from the first EditText created (top left EditText). But I can not get text from the other EditText's when entering text in to them.

Question: What is the correct procedure for adding the addTextChangedListener() to multiple EditTexts within a TableLayout? By time of creation or later? If later - how can I then access the EditTexts individually the best way?

Any suggestions on fixing my code will be appreciated.

public void inflateView(String[] names){

    tableLayout = (TableLayout)findViewById(R.id.table1);
    TableRow tableRow;
    TextView textView;
    EditText[][] eTxts = new EditText[names.length-1][rows.length-1];

    for (int i = 0; i < rows.length; i++) {
        tableRow = new TableRow(getApplicationContext());

        textView = new TextView(getApplicationContext());
        textView.setText(rows[i]);
        textView.setTextSize(20);
        textView.setId(i);
        textView.setPadding(20, 20, 20, 20);

        tableRow.addView(textView);

        if(i<1) {
            for (int j = 0; j < names.length; j++) {
                textView = new TextView(getApplicationContext());
                textView.setText(names[j]);
                textView.setTextSize(20);
                textView.setId(j);
                textView.setPadding(20, 20, 20, 20);

                tableRow.addView(textView);
            }
        }

        else{
            for (int j = 0; j < names.length; j++) {
                edit = new EditText(getApplicationContext());
                edit.setText("");
                edit.setTextSize(20);
                edit.setId(j);
                //eTxts[i][j] = edit;
                edit.setPadding(20, 20, 20, 20);
                final int jj = j; //Der kan være en fejl i final
                edit.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        Log.d("Entered TextWatcher", jj+"");

                        //points points points
                        int plottedPoint = 0;
                        for (int i = 0; i < tableLayout.getChildCount(); i++) {
                            TableRow mRow = (TableRow) tableLayout.getChildAt(i); //Det er derfor den kun tager den første.
                            Object et = mRow.getChildAt(i);
                            //TextView mTextView = (TextView) mRow.getChildAt(1);
                            if(et instanceof  EditText) {
                                EditText e = (EditText) et;
                                if(!e.getText().toString().equals("")){
                                    try {
                                        plottedPoint = Integer.parseInt(e.getText().toString());
                                        Log.i("edittext value : ", e.getText().toString());
                                    } catch(Exception ex){
                                        Toast.makeText(getApplicationContext(), "Not a number", Toast.LENGTH_SHORT);
                                        ex.printStackTrace();
                                    }
                                }

                            }
                        }
                    }
                });

                tableRow.addView(edit);
            }
        }

        tableLayout.addView(tableRow);

    }
}
Natalie
  • 43
  • 2
  • 8

1 Answers1

0

I found the solution.

I added a inner loop in the following code:

    //points points points
                        int plottedPoint = 0;
                        for (int i = 0; i < tableLayout.getChildCount(); i++) {
                            TableRow mRow = (TableRow) tableLayout.getChildAt(i);
    //here
                            for(int a = 0; a < mRow.getChildCount(); a++){
                                Object et = mRow.getChildAt(a);
                                //TextView mTextView = (TextView) mRow.getChildAt(1);
                                if(et instanceof  EditText) {
                                    EditText e = (EditText) et;
                                    if(!e.getText().toString().equals("")){
                                        try {
                                            plottedPoint = Integer.parseInt(e.getText().toString());
                                            Log.i("edittext value : ", e.getText().toString());
                                        } catch(Exception ex){
                                            Toast.makeText(getApplicationContext(), "Not a number", Toast.LENGTH_SHORT);
                                            ex.printStackTrace();
                                        }
                                    }

                                }
                            }

                        }
Natalie
  • 43
  • 2
  • 8