Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Two dimensional ArrayList Help

Currently I have my code putting user input into a one-dimensional ArrayList, but I would like to put them into a two dimensional ArrayList and am having some trouble.

Here is my code:

public class Game extends Activity implements OnClickListener {
   private static final String TAG = "Matrix";
   static ArrayList<EditText> columnEditTexts;




   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       this.setContentView(R.layout.matrix);
       View doneButton = findViewById(R.id.done_button);
       doneButton.setOnClickListener(this);
       columnEditTexts = new ArrayList<EditText>();

       for(int i = 0; i < MatrixMultiply.h1; i++){
           TableLayout table = (TableLayout)findViewById(R.id.myTableLayout);
           TableRow row = new TableRow(this);
           EditText column = new EditText(this);
           for(int j = 0; j < MatrixMultiply.w1; j++){
               table = (TableLayout)findViewById(R.id.myTableLayout);
               column = new EditText(this);
               column.setId(i);
               row.addView(column);
               columnEditTexts.add(column);
           }
           table.addView(row);
       }



   }
like image 676
Biggsy Avatar asked Jan 21 '23 08:01

Biggsy


1 Answers

Well you need to first create a two dimensional ArrayList. To do that, you need to create an ArrayList of ArrayLists.

ArrayList<ArrayList<EditText>> arrayOfEditTexts = new ArrayList<ArrayList<EditText>>();

So then you loop will become something along these lines (assuming I understand what you are trying to do):

for(int i = 0; i < MatrixMultiply.h1; i++){
       columnEditTexts = new ArrayList<EditText>();
       TableLayout table = (TableLayout)findViewById(R.id.myTableLayout);
       TableRow row = new TableRow(this);
       EditText column = new EditText(this);
       for(int j = 0; j < MatrixMultiply.w1; j++) {               
           column = new EditText(this);
           column.setId(i);
           row.addView(column);
           columnEditTexts.add(column);
       }
       table.addView(row);
       arrayOfEditTexts.add(columnEditTexts);
   }
like image 198
Corey Sunwold Avatar answered Jan 30 '23 19:01

Corey Sunwold