Need Help with Referencing Dynamically Created Inputs
Hello everyone, I am trying to build an android app that will allow the user to create two matrices (A and B) and then perform some operation to get Matrix C IE (A+B=C). The code down below pulls the size of the two arrays from another class. The Class then creates Input boxes for the respective size of the matrix. The problem I am having is referencing the dynamically created matrices so that when the user hits a button Array C can pull values from the created input boxes. If my question is unclear please comment, and I can try to clarify as best as I can. My code is below.
Code java:
package zach.etier.osu.MC;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
public class Matrix extends Activity implements OnClickListener {
LinearLayout A,B,C,Container;
String gotBread,gotBread2,gotBread3,gotBread4;
int Arow,Acol,Brow,Bcol;
Button infocalc;
TableRow rowA,rowB;
String[] myTextViewsA, myTextViewsB;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
infocalc=(Button)findViewById(R.id.calc);
infocalc.setOnClickListener(this);
A=(LinearLayout)findViewById(R.id.arrayA);
B=(LinearLayout)findViewById(R.id.arrayB);
C=(LinearLayout)findViewById(R.id.arrayC);
Container=(LinearLayout)findViewById(R.id.Container);
Container.setVisibility(View.INVISIBLE);
Bundle gotBasket =getIntent().getExtras();
gotBread=gotBasket.getString("key");
Arow=Integer.parseInt(gotBread);
gotBread2=gotBasket.getString("key2");
Acol=Integer.parseInt(gotBread2);
gotBread3=gotBasket.getString("key3");
Brow=Integer.parseInt(gotBread3);
gotBread4=gotBasket.getString("key4");
Bcol=Integer.parseInt(gotBread4);
final EditText[] myTextViewsA = new EditText[Arow];
for (int i = 0; i < Arow; i++) {
TableRow rowA = new TableRow(this);
rowA.setId(i);
for (int j = 0; j < Acol; j++) {
EditText cell = new EditText(this);
cell.setHint("(" + i + ", " + j + ")");
rowA.addView(cell);
myTextViewsA[j] = cell;
}
A.addView(rowA, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
rowA.setGravity(Gravity.CENTER);
}
final EditText[] myTextViewsB = new EditText[Brow];
for (int i = 0; i < Brow; i++) {
TableRow rowB = new TableRow(this);
rowB.setId(i);
for (int j = 0; j < Bcol; j++) {
EditText cell = new EditText(this);
cell.setHint("(" + i + ", " + j + ")");
rowB.addView(cell);
myTextViewsB[i] = cell;
}
B.addView(rowB, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
rowB.setGravity(Gravity.CENTER);
}
Container.setVisibility(View.VISIBLE);
}
public void onClick(View v) {
// TODO Auto-generated method stub
final EditText[] myTextViewsC = new EditText[Arow];
for (int i = 0; i < Arow; i++) {
TableRow rowC = new TableRow(this);
for (int j = 0; j < Acol; j++) {
EditText cell = new EditText(this);
cell.setText("(" + i + ", " + j + ")");
rowC.addView(cell);
}
C.addView(rowC, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
rowC.setGravity(Gravity.CENTER);
}
}
}
Re: Need Help with Referencing Dynamically Created Inputs
Hi and welcome.
Please see the announcements page for the use of code tags when posting code.
Quote:
If my question is unclear please comment,
Your question is unclear. Does the code compile and run? Post any error messages you may be getting. Explain what the code does do if it does run, and explain what you are stuck on. Trouble with the listeners? Trouble with passing information and if so from where to where? Trouble getting values from input boxes? Giving great details about exactly where you are having problems makes it much easier to browse the code and offer a suggestion.
Re: Need Help with Referencing Dynamically Created Inputs
Quote:
Originally Posted by
jps
Your question is unclear. Does the code compile and run? Post any error messages you may be getting. Explain what the code does do if it does run, and explain what you are stuck on. Trouble with the listeners? Trouble with passing information and if so from where to where? Trouble getting values from input boxes? Giving great details about exactly where you are having problems makes it much easier to browse the code and offer a suggestion.
Thank you jps, I am sorry for the unclear question I will try to elaborate. The code I posted will compile and run, however if you copy and paste this code it will not run. This is because I am importing the variables Arow,Acol,Brow,Bcol from another class. Because the code will run for me there are no error logs. My problem is that once the edit texts for the two arrays are created I'm not sure how to 1) Set there ID and 2) call there ID to get there values later in the program.
Re: Need Help with Referencing Dynamically Created Inputs
You can set the id through java.
In eclipse you can easily set the id of an element with the properties window.
You can also edit the xml directly as a text file or by using the tool that comes with the ADK.
You can use View.getid() to get the id. Seriously read the entire page linked to here. There is a wealth of information on the long page, but read it. Not just what is on that page but the related links give a great idea of other possibilities you may not be aware exists.
On a side note, anyone learning android should pick one semi-random page from the docs to read every day.