I have a TableRow that is loaded dynamically. It contains multiple EditTexts and CheckBoxes. How do I get the user input from these EditTexts and CheckBoxes?
This my Code
public void cargarProductos(SQLiteDatabase db){
TableLayout tabla = (TableLayout) findViewById(R.id.Detalle);
Cursor cProd = db.rawQuery("SELECT * FROM vstEncProductos WHERE IdCliente="+idCliente+" AND Idcategoria="+IdCategoria, null);
int ValorChk = 0;
if (cProd.moveToFirst()) {
do {
TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
row.setId(cProd.getInt(0));
ImageButton btnBarCode = new ImageButton(this);
btnBarCode.setBackgroundResource(R.drawable.barcodereader48);
btnBarCode.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.setPackage("com.google.zxing.client.android");
startActivityForResult(intent, 0);
};
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
showDialog(R.string.result_succeeded, "Format: " + format + "\nContents: " + contents);
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
});
row.addView(btnBarCode, new TableRow.LayoutParams(0));
TextView txtProducto = new TextView(this);
txtProducto.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
txtProducto.setText(cProd.getString(1));
txtProducto.setWidth(230);
row.addView(txtProducto, new TableRow.LayoutParams(1));
EditText txtPrecio = new EditText(this);
txtPrecio.setWidth(130);
txtPrecio.setInputType(InputType.TYPE_CLASS_NUMBER);
row.addView(txtPrecio, new TableRow.LayoutParams(2));
TextView txtPrecioAnt = new TextView(this);
txtPrecioAnt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
txtPrecioAnt.setText(cProd.getString(7));
txtPrecioAnt.setWidth(115);
txtPrecioAnt.setGravity(Gravity.RIGHT);
row.addView(txtPrecioAnt, new TableRow.LayoutParams(3));
CheckBox chkPresencia = new CheckBox(this);
ValorChk=cProd.getInt(4);
chkPresencia.setChecked(ValorChk==-1);
chkPresencia.setWidth(50);
row.addView(chkPresencia, new TableRow.LayoutParams(4));
CheckBox chkPrecioPromo = new CheckBox(this);
ValorChk=cProd.getInt(4);
chkPrecioPromo.setChecked(ValorChk==-1);
chkPrecioPromo.setWidth(50);
row.addView(chkPrecioPromo, new TableRow.LayoutParams(4));
CheckBox chkAlerta = new CheckBox(this);
ValorChk=cProd.getInt(4);
chkAlerta.setChecked(ValorChk==-1);
chkAlerta.setWidth(50);
row.addView(chkAlerta, new TableRow.LayoutParams(4));
tabla.addView(row, new TableLayout.LayoutParams());
} while (cProd.moveToNext());
}
cProd.close();
I'm assuming you're using some sort of adapter class to load the data into your table. When you create a view, simply assign an OnClickListener to the checkboxes and a TextWatcher to the EditTexts. Use the callbacks from these classes to do what ever it is you want with the users input. Am I understanding your question correctly?
when you are creating new tablerows you can save each file in a multi vector of, for example, strings. Then you only must get size of each vector inside theparent vector. My example with your cody modified:
Vector<Vector<String>> result = new Vector<Vector<String>>();
Vector<String> vecAux = new Vector<String>();
TextView txtProducto = new TextView(this);
txtProducto.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
txtProducto.setText(cProd.getString(1));
txtProducto.setWidth(230);
row.addView(txtProducto, new TableRow.LayoutParams(1));
vecAux.add(cProd.getString(1));
EditText txtPrecio = new EditText(this);
txtPrecio.setWidth(130);
txtPrecio.setInputType(InputType.TYPE_CLASS_NUMBER);
row.addView(txtPrecio, new TableRow.LayoutParams(2));
TextView txtPrecioAnt = new TextView(this);
txtPrecioAnt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
txtPrecioAnt.setText(cProd.getString(7));
txtPrecioAnt.setWidth(115);
txtPrecioAnt.setGravity(Gravity.RIGHT);
row.addView(txtPrecioAnt, new TableRow.LayoutParams(3));
vecAux.add(cProd.getString(7));
CheckBox chkPresencia = new CheckBox(this);
ValorChk=cProd.getInt(4);
chkPresencia.setChecked(ValorChk==-1);
chkPresencia.setWidth(50);
row.addView(chkPresencia, new TableRow.LayoutParams(4));
CheckBox chkPrecioPromo = new CheckBox(this);
ValorChk=cProd.getInt(4);
chkPrecioPromo.setChecked(ValorChk==-1);
chkPrecioPromo.setWidth(50);
row.addView(chkPrecioPromo, new TableRow.LayoutParams(4));
CheckBox chkAlerta = new CheckBox(this);
ValorChk=cProd.getInt(4);
chkAlerta.setChecked(ValorChk==-1);
chkAlerta.setWidth(50);
row.addView(chkAlerta, new TableRow.LayoutParams(4));
tabla.addView(row, new TableLayout.LayoutParams());
result.add(vecAux);
And get results looking for all position of the vector:
String texto = "";
for (int i = 0; i < result.size(); i++) {
for (int j = 0; j < result.elementAt(i).size(); j++) {
texto += result.elementAt(i).elementAt(j) + "\t";
}
}
For get if checkbox is checked or not you can get it by ID (findViewById).
I hope help you, bye bye ;-)
Related
I'm new to Android Studio. I would like to read input from user from a dynamically created elements. I've already managed to create the elements. Now I do not know at which place should I place the reading element. Can someone give me a MWE here ? I do not know also if I should use getText(this) or editText(this) or something else.
public void click(View view) {
String col1;
String col2;
TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1);
TableRow row = new TableRow(this);
TextView tv = new TextView(this);
TextView c = new TextView(this);
EditText etUserInfoNewValue = (EditText)findViewById(R.id.simpleEditText);
tv.setText("This is text");
tl.addView(row);
row.addView(tv);
for (int x = 0; x < 5; x++) {
col1 = "(" + x + ")"+Integer.toBinaryString(x);
col2 = "1";
TableRow newRow = new TableRow(this);
TextView column1 = new TextView(this);
TextView column2 = new TextView(this);
EditText editText1 = new EditText(this);
TextView column3 = new TextView(this);
column1.setText(col1);
column1.setText(col1);
column2.setText(col2);
newRow.addView(column1);
newRow.addView(editText1);
newRow.addView(column3);
tl.addView(newRow, new TableLayout.LayoutParams());
}
}
I have this
public void header(int recursocabecera)
{
TableRow.LayoutParams layoutCelda;
TableRow fila = new TableRow(actividad);
TableRow.LayoutParams layoutFila = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
fila.setLayoutParams(layoutFila);
String[] arraycabecera = rs.getStringArray(recursocabecera);
COLUMNAS = arraycabecera.length;
for(int i = 0; i < arraycabecera.length; i++)
{
TextView texto = new TextView(actividad);
// layoutCelda = new TableRow.LayoutParams(obtenerAncho(arraycabecera[i]), LayoutParams.FILL_PARENT);
layoutCelda = new TableRow.LayoutParams(obtenerAnchoPixelesTexto(arraycabecera[i]), LayoutParams.FILL_PARENT);
// layoutCelda= new LayoutParams(100, ViewGroup.LayoutParams.FILL_PARENT);
texto.setSingleLine(true);
texto.setText(arraycabecera[i]);
texto.setGravity(Gravity.CENTER_HORIZONTAL);
texto.setRotation(-90);
texto.setTextAppearance(actividad, R.style.estilo_celda2);
texto.setBackgroundResource(R.drawable.tabla_celda_cabecera2);
texto.setLayoutParams(layoutCelda);
texto.setPadding(20,0,0,100);
layoutCelda.setMargins(0,0,60,0);
layoutCelda.setMarginEnd(90);
fila.addView(texto);
}
tabla.addView(fila);
filas.add(fila);
FILAS++;
}
the result is somenthing like that
but I need it to be something like this
setGravity it works half, but the titles are shown cropped
My result:
I want to set user input to the textviews in a tablelayout for a word game application.i am new to android please help me through this.
tl = (TableLayout)findViewById(R.id.table);
TableLayout.LayoutParams tablelparam = new TableLayout.LayoutParams(30,30);
for (int r = 1; r <=9; r++) {
r1= new TableRow(this);
r1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
for (int c = 1; c<=5; c++) {
TextView txtview=new TextView(MainActivity.this);
txtview.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
txtview.setId(counter);
counter++;
txtview.setText( +counter + inputWord);
r1.addView(txtview);
}
tl.addView(r1,new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));
}
Submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ArrayList<String>words = new ArrayList<String>();
for (int j = 0; j < words.size(); j++) {
TextView txtview = new TextView(MainActivity.this);
txtview.setId(j);
txtview.setText("" + words.get(j));
txtview.getLayoutParams();
t3.setText(t3.getText() + " " + txtview.getText());
}
};
Set the onClickListener() for each TextView you instantiate inside your for loop.
I have developed a form in dynamically using some edit text,check boxes,radio buttons and buttons.I have created form successfully ,but how i can get the values from that and stored in database.Please can any one help me.
Thanking in Advance.
public void textView() {
idText++;
i++;
LinearLayout linearLayoutHorizantal1 = new LinearLayout(
getApplicationContext());
linearLayoutHorizantal1.setOrientation(LinearLayout.HORIZONTAL);
/*
* LinearLayout.LayoutParams params = new
* LinearLayout.LayoutParams(LayoutParams
* .WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
* linearLayoutHorizantal1.setGravity(Gravity.RIGHT);
*/
txtQuetion = new TextView(getApplicationContext());
txtQuetion.setText(i + ". " + strQuestionText);
txtQuetion.setTextColor(Color.BLACK);
txtQuetion.setTextSize(20);
txtQuetion.setId(idText);
linearLayoutHorizantal1.addView(txtQuetion);
linearLayoutDynamicAdd.addView(linearLayoutHorizantal1);
}
public void RadioButtons() {
radiogroup = new RadioGroup(getApplicationContext());
radiogroup.setOrientation(RadioGroup.HORIZONTAL);
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
SurveyFillActivity.this, null);
params.setMargins(10, 10, 10, 0);
linearLayoutDynamicAdd.addView(radiogroup);
for (int j = 0; j < answersList.length; j++) {
if (answersList[j] != null) {
idRadio++;
rb = new RadioButton(getApplicationContext());
rb.setId(idRadio);
rb.setText(answersList[j]);
rb.setTextColor(Color.BLACK);
rb.setButtonDrawable(R.drawable.radio_custom);
// rb.setChecked(true);
rb.setLayoutParams(params);
radiogroup.addView(rb);
}
}
}
private EditText editText(int _intID) {
idEditBox++;
final LayoutParams lparams = new LayoutParams(350, 50);
final EditText et = new EditText(this);
lparams.leftMargin = 20;
lparams.topMargin = 10;
et.setLayoutParams(lparams);
et.setWidth(32);
et.setEms(50);
et.setBackgroundResource(R.drawable.customborder_backbutton);
/*
* EditText et = new EditText(getApplicationContext());
* et.setId(idEditBox); et.setHeight(60); et.setWidth(50);
*/
//et.setBackgroundColor(Color.WHITE);
String s1 = et.getText().toString();
linearLayoutDynamicAdd.addView(et, lparams);
return et;
}
The above code for creating forms and i need to get the values form it and store in Database.
Create the arrays for texviews and radiobuttons ...what ever you need in your form.
take this as reference
TextView tv[] = new TextView[HowManyYouNeed];
TextView tvsa = new TextView(YourContax);
tv[position]=tvsa;//position like o ..1..2
tv[0].getText();
May be help full..
i want to set the calculation result in TextView named total[] from the value of EditText named point[] .but i am able to parse the value and put it in the TextView . it give me an error in logcat
06-27 02:27:01.467: E/AndroidRuntime(275): Caused by: java.lang.NumberFormatException: unable to parse '' as integer
this is an error in logcat. i want to add integer values to the textview continuously from the edittext on the same layout
public class player_name extends Activity {
LinearLayout player_name;
TableLayout ply_name;
Bundle b,b1;
List<TextView> allEds = new ArrayList<TextView>();
List<Button> allplus = new ArrayList<Button>();
List<Button> allminus = new ArrayList<Button>();
List<EditText> alledit = new ArrayList<EditText>();
List<TextView> alltotal = new ArrayList<TextView>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player_name);
b1 = getIntent().getExtras();
String[] result = b1.getStringArray("playerName");
player_name = (LinearLayout) findViewById(R.id.player_name);
ply_name = new TableLayout(this);
player_name.addView(ply_name);
TableLayout.LayoutParams tableRowParams=new TableLayout.LayoutParams
(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT,1.0f);
TextView[] ed1 = new TextView[result.length+1];
Button[] plus = new Button[result.length+1];
Button[] minus = new Button[result.length+1];
EditText[] point = new EditText[result.length+1];
TextView[] total = new TextView[result.length+1];
TableRow[] TR= new TableRow[result.length+1];
int[] totalscore = null;
String[] temp = null;
Button btnResult = new Button(player_name.this);
btnResult.setText(" click here to get RESULT");
for(int i=0;i<=(result.length-1);i++) {
ed1[i] = new TextView(player_name.this);
plus[i] = new Button(player_name.this);
minus[i] = new Button(player_name.this);
point[i] = new EditText(player_name.this);
total[i] = new TextView(player_name.this);
TR[i] = new TableRow(player_name.this);
allEds.add(ed1[i]);
alltotal.add(total[i]);
alledit.add(point[i]);
allplus.add(plus[i]);
allminus.add(minus[i]);
TR[i].addView(ed1[i]);
TR[i].addView(point[i]);
TR[i].addView(plus[i]);
TR[i].addView(minus[i]);
TR[i].addView(total[i]);
ply_name.addView(TR[i]);
TR[i].setLayoutParams(tableRowParams);
totalscore[i] =Integer.parseInt(point[i].getText().toString());
temp[i] = "" + totalscore[i];
ed1[i].setId(i);
ed1[i].setHeight(50);
ed1[i].setWidth(70);
ed1[i].setText(result[i]);
ed1[i].setTextColor(Color.CYAN);
total[i].setId(i);
total[i].setHeight(50);
total[i].setWidth(70);
total[i].setText(""+0);
total[i].setTextColor(Color.CYAN);
point[i].setId(i);
point[i].setHeight(50);
point[i].setWidth(120);
point[i].setHint(result[i]+"\'s");
point[i].setInputType(InputType.TYPE_CLASS_NUMBER);
point[i].setTextColor(Color.BLACK);
plus[i].setId(i);
plus[i].setHeight(50);
plus[i].setWidth(50);
plus[i].setText("+");
plus[i].setTextColor(Color.BLACK);
minus[i].setId(i);
minus[i].setHeight(50);
minus[i].setWidth(50);
minus[i].setText("-");
minus[i].setTextColor(Color.BLACK);
plus[i].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
minus[i].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
}
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
player_name.addView(btnResult, lp);
btnResult.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent1 = new Intent(player_name.this,result.class);
startActivity(intent1);
}
});
}
}
You need to check whether the string you are parsing is an integer. Try this code:
if (IsInteger(point[i].getText().toString()))
totalscore[i] =Integer.parseInt(point[i].getText().toString());
and add this function:
public static boolean IsInteger(String s)
{
if (s == null || s.length() == 0) return false;
for(int i = 0; i < s.length(); i++)
{
if (Character.digit(s.charAt(i), 10) < 0)
return false;
}
return true;
}
I hope this helps