get inputs in an edittext dynamically and pass that to next screen - android

This is my code..I have created a dynamic screen where i am generating one textview and edittext under a for loop.but after button click it is only getting the last input.I need to get all the inputs of editText and have to pass them to new screen..guyss..plzz help me out.here is my code..
runOnUiThread(new Runnable() {
public void run() {
final LinearLayout findViewById = (LinearLayout) findViewById(R.id.dynamicInputs);
// TextView textView = (TextView) findViewById(R.id.name);
TextView textView = new TextView(Activity_UserInput.this);
textView.setText(map.get(KEY_NAME) + " :");
textView.setTextColor(Color.BLACK);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
findViewById.addView(textView);
final EditText editText = new EditText(
Activity_UserInput.this);
editText.setText("");
editText.setFocusableInTouchMode(true);
editText.requestFocus();
findViewById.addView(editText);
final ArrayList<EditText> allEds = new ArrayList<EditText>();
allEds.add(editText);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// it = new Intent(Activity_UserInput.this,
// Submition_Activity.class);
// it.putExtra("input", arryList);
// System.out.println("get the input"
// + arryList);
String[] string = new String[allEds.size()];
for (int i = 0; i < string.length; i++) {
string[i] = allEds.get(i).getText().toString();
}
}
});
}
});

Here, in your code arraylist of EditText created every time.So initialize allEds at startup
final ArrayList<EditText> allEds = new ArrayList<EditText>();
and then add EditText to arraylist
final EditText editText = new EditText( Activity_UserInput.this);
editText.setText("");
editText.setFocusableInTouchMode(true);
editText.requestFocus();
findViewById.addView(editText);
allEds.add(editText);

You are creating a new allEds everytime you put final ArrayList<EditText> allEds = new ArrayList<EditText>(); So the allEds will contain just one element.
Try creating it outside the for loop.

You have created only one edittext and also you have added only one edittext to the allEds arraylist. You have also declared the size of the string[] to be the size of the allEds arraylist. So, the length of string[] will be 1 only and the for loop will execute only once. so that, you will get only one input.
If you want to create multiple edittext create them inside a for loop. Or if you are already creating them under a for loop try to declare the following line outside the loop instead of declaring inside.
{ArrayList<EditText> allEds = new ArrayList<EditText>();}

My Layout :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/mTopLayout" ></LinearLayout>
Within my activity
LinearLayout mLayout = (LinearLayout) findViewById(R.id.mTopLayout);
ArrayList<EditText> mCheck = new ArrayList<EditText>();
EditText mEdit = new EditText(MainActivity.this);
mEdit.setText("");
mEdit.setFocusableInTouchMode(true);
mEdit.requestFocus();
mLayout.addView(mEdit);
mCheck.add(mEdit);
EditText mEdit1 = new EditText(MainActivity.this);
mEdit1.setText("");
mEdit1.setFocusableInTouchMode(true);
mEdit1.requestFocus();
mLayout.addView(mEdit1);
mCheck.add(mEdit1);
EditText mEdit2 = new EditText(MainActivity.this);
mEdit2.setText("");
mEdit2.setFocusableInTouchMode(true);
mEdit2.requestFocus();
mLayout.addView(mEdit2);
mCheck.add(mEdit2); ......
Within onClick method
String[] st = new String[mCheck.size()];
for(int i=0;i<st.length;i++){
st[i] = mCheck.get(i).getText().toString();
}
Log.i("Recived String", st.length+"");
for(int j=0;j<st.length;j++){
Log.i(" String", st[j]+""+j); }
It works fine for me....Please review your layout and let me know.... thanks...

Related

How to create array of a specific view and add each of them to main layout in Android

I am able to run following code without any issue
EditText[] course_name = new EditText[4];
OnCreate {
ConstraintLayout layout = findViewById(R.id.layout_setupclass);
course_name[0] = new EditText(this);
course_name[0] = (EditText) LayoutInflater.from(this).inflate(R.layout.edittext, null);
layout.addView(course_name[0]);
}
But app would crash when I try this
EditText[] course_name = new EditText[4];
OnCreate {
ConstraintLayout layout = findViewById(R.id.layout_setupclass);
for(int i=0; i<5; i++)
{
course_name[i] = new EditText(this);
course_name[i] = (EditText) LayoutInflater.from(this).inflate(R.layout.edittext, null);
layout.addView(course_name[i]);
}
}
you are creating array with 4 elements capacity new EditText[4];, but in your iteration you have 5 in-for-loop calls (i=0 to 4). index 4 doesn't exists in array (arrays are 0-based indexed), so you are getting ArrayIndexOutOfBoundsException on course_name[i] call (when i=4, as max may be 3 due to size of array)
btw. calling course_name[i] = new EditText(this); is redundant, as in very next line you are attaching another (inflated from XML) view to this variable

How to save data from dynamically created edittext at runtime to sqlite database?

I am building a program that creates 6 edit Text at run-time and I want to save the data input by the user in those edittexts into sqlite Database but I'm getting an error. and i provided the code that is creating my edit texts i hope i can get some help please. the error is in the second case.
the edittext is may have not been initialized.
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.buttonAddIntervention:
final TableLayout table = (TableLayout)findViewById(R.id.tableinterventions);
EditText ed = new EditText(this);
TextView tv = new TextView(this);
final TableRow tabr = new TableRow(this);
tabr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv.setText("Code");
ed.setHint("Code");
ed.setText("", null);
ed.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tabr.addView(tv);
tabr.addView(ed);
table.addView(tabr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
EditText ed1 = new EditText(this);
TextView tv1 = new TextView(this);
Button b1 = new Button(this);
final TableRow tabr1 = new TableRow(this);
tabr1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
b1.setText("-");
tv1.setText("Start Time");
ed1.setHint("Start Time");
ed1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
b1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tabr1.addView(tv1);
tabr1.addView(ed1);
tabr1.addView(b1);
table.addView(tabr1, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
EditText ed2 = new EditText(this);
TextView tv2 = new TextView(this);
final TableRow tabr2 = new TableRow(this);
tabr2.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv2.setText("End Time");
ed2.setHint("End Time");
ed2.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv2.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tabr2.addView(tv2);
tabr2.addView(ed2);
table.addView(tabr2, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT) );
EditText ed3 = new EditText(this);
TextView tv3 = new TextView(this);
final TableRow tabr3 = new TableRow(this);
tabr3.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv3.setText("Description");
ed3.setHint("Description");
ed3.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv3.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tabr3.addView(tv3);
tabr3.addView(ed3);
table.addView(tabr3,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
EditText ed4 = new EditText(this);
TextView tv4 = new TextView(this);
final TableRow tabr4 = new TableRow(this);
tabr4.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv4.setText("Product");
ed4.setHint("Product");
ed4.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv4.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tabr4.addView(tv4);
tabr4.addView(ed4);
table.addView(tabr4,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
EditText ed5 = new EditText(this);
TextView tv5 = new TextView(this);
final TableRow tabr5 = new TableRow(this);
tabr5.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv5.setText("Serial Number");
ed5.setHint("Serial Number");
ed5.setLayoutParams(new TableRow.LayoutParams(190,LayoutParams.WRAP_CONTENT));
tv5.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tabr5.addView(tv5);
tabr5.addView(ed5);
table.addView(tabr5,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
b1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v1) {
// TODO Auto-generated method stub
table.removeView(tabr);
table.removeView(tabr1);
table.removeView(tabr2);
table.removeView(tabr3);
table.removeView(tabr4);
table.removeView(tabr5);
}
});
break;
case R.id.BtnTestSaveAll:
InterventionsDatabase idb1 = new InterventionsDatabase();
//the error is here the ed,ed1,ed2,ed3,ed4 and ed5 may not have been initialized
String cod= String.valueOf(ed.getText());
String s_time = String.valueOf(ed1.getText().toString());
String e_time = String.valueOf(ed2.getText().toString());
String d = String.valueOf(ed3.getText().toString());
String p = String.valueOf(ed4.getText().toString());
String Sn = String.valueOf(ed5.getText().toString());
idb1.setEmp_code(Integer.parseInt(cod));
idb1.setEmp_startTime(Integer.parseInt(s_time));
idb1.setEmp_endTime(Integer.parseInt(e_time));
idb1.setDescription(d);
idb1.setProduct(p);
idb1.setSerialNumber(Integer.parseInt(Sn));
Dbhelper.createInterventionsDB(Integer.parseInt(cod),Integer.parseInt(s_time),Integer.parseInt(e_time), d, p,Integer.parseInt(Sn));
Toast.makeText(getApplicationContext(), "Yeah", Toast.LENGTH_SHORT).show();
break;
The "may have not been initialized" error appears when you want to access object which... may have not been initialized. It means that for example this code won't work:
int a = 5;
EditText et;
if(a == 5) {
et = new EditText();
}
et.setText("some text");
It won't work, because et is initialized inside if statement, which condition in this case of course is passed, but the compiler doesn't know that. It's just more safe to show you that you made a mistake here than eventually having your application crashed on run-time.
Edit:
You're initializing EditText in Button's onClick() (therefore when the Button is clicked) and you want to access it somewhere else (when the other Button is clicked). Compiler prevents this, because it must be sure that EditText is initialized, it can't rely on user to click the initializing button first.
In order to solve this problem, create those EditText's to be fields of your Activity class and initialize them in onCreate(), there's no need to do this when the Button is clicked. And that's it.
Construct like this:
switch (var) {
case 1:
SomeType foo = new SomeType();
// ...
break;
case 2:
foo.something();
// ...
break;
}
has the problem that foo is not initialized when case 2 is executing. Variables are visible in their declaring curly {} scope, i.e. the switch in this case. However, the code execution path, when case 2 is taken, doesn't include the initialization of the variable. The compiler is smart enough to notice this and gives you a warning.
In your case a good solution is to move the EditText variable declarations to class member level:
private EditText mEd1;
// ...
mEd1 = new EditText(this);
and when referencing them, check for non-null
if (mEd1 != null) {
// ...
String s_time = String.valueOf(mEd1.getText().toString());
You have to get view of all edittext first then do whatever you want something like this:
ViewGroup group = (ViewGroup)findViewById(R.id.your_layout);
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
((EditText)view).setText("");
}
}

Cant get the value of an Edittext [duplicate]

This question already has answers here:
Android getText from EditText field
(6 answers)
Closed 8 years ago.
hi i made a program that have EditText... i used for loop to set the number of my EditText, i cant get the value of my EditText(excellent), how can i do that? please help me..my output goes this way...
i want that 3,2,5,1,4 will display in my EditText,,
here are my codes...
final TableLayout table = new TableLayout(getApplicationContext());
table.setVerticalScrollBarEnabled(true);
table.setPadding(10, 10, 10, 10);
TableRow tableRow = new TableRow (getApplicationContext());
TextView txt = new TextView (getApplicationContext());
TextView txt2 = new TextView (getApplicationContext());
TextView txt3 = new TextView (getApplicationContext());
TextView txt4 = new TextView (getApplicationContext());
TextView txt5 = new TextView (getApplicationContext());
TextView txt6 = new TextView (getApplicationContext());
tableRow.addView(txt);
tableRow.addView(txt2);
tableRow.addView(txt3);
tableRow.addView(txt4);
tableRow.addView(txt5);
tableRow.addView(txt6);
tableRow.setBackgroundColor(Color.GRAY);
txt.setText("Question ");
txt2.setText("Excellent ");
txt3.setText("Best ");
txt4.setText("Better ");
txt5.setText("Good ");
txt6.setText("Poor ");
txt.setTextColor(Color.BLACK);
txt2.setTextColor(Color.BLACK);
txt3.setTextColor(Color.BLACK);
txt4.setTextColor(Color.BLACK);
txt5.setTextColor(Color.BLACK);
txt6.setTextColor(Color.BLACK);
table.addView(tableRow);
TableRow tableRow2 = null;
EditText excellent = null;
EditText best = null;
EditText better = null;
EditText good = null;
EditText poor = null;
TextView name = null;
int j=0;
for(j = 1; j<=count; j++){
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
tableRow2 = new TableRow (getApplicationContext());
excellent = new EditText (getApplicationContext());
best = new EditText (getApplicationContext());
better = new EditText (getApplicationContext());
good = new EditText (getApplicationContext());
poor = new EditText (getApplicationContext());
name = new TextView (getApplicationContext());
//i want to retrive the value of this --->//excellent.setBackgroundColor(color);
best.setBackgroundColor(color);
better.setBackgroundColor(color);
good.setBackgroundColor(color);
poor.setBackgroundColor(color);
name.setText("Q#"+Integer.toString(j));
tableRow2.addView(name);
tableRow2.addView(excellent);
tableRow2.addView(best);
tableRow2.addView(better);
tableRow2.addView(good);
tableRow2.addView(poor);
table.addView(tableRow2);
}
final StringBuilder output = new StringBuilder();
final String[] a = excellent.getText().toString().split(",");
output.append(a+",");
TableRow tableRow1 = new TableRow (getApplicationContext());
Button get = new Button(getApplicationContext());
tableRow1.addView(get);
get.setText("Get!");
get.setTextSize(8);
//******************************************************************************//
// GET! //
//******************************************************************************//
get.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditText x = new EditText (getApplicationContext());
x.setText(output);
table.addView(x);
}
});
//******************************************************************************//
// END OF GET! //
//******************************************************************************//
The problem is that this
output.append(a+",");
Prints a representation of the object a, not the strings that a contains. Try somthing like this:
for(String s : a){
output.append(s);
}
There are multiple things going wrong here.
you split on "," but that is not what you want. you want the excellent.getText().toString(); of the EditTexts
You append the text it is just after creation. It will be empty string at that time.
What you want to do
Make an ArrayList of EditText
Put all excellent EditTexts in it.
In the onClick go through this list and append all the getText().toString() of these EditTexts
I won't give you an exact implementation. You should be able to figure that out on your own.
Your split function should be followed by a FOR loop. Split gives you a array.
So you need to iterate the array and append inside the lopp. Example
String str = "one-two-three";
for (String val: str.split("-", 2)){
// Append your output with val here
}
Cheers

android Edit Text Customization

for (int i = 0; i < sub.length; i++) {
tr[i] = new TableRow(this);
EditText et = new EditText(this);
et.setText(message2);
tr[i].addView(et);
EditText et1 = new EditText(this);
et1.setText(sub[i]);
tr[i].addView(et1);
EditText et2 = new EditText(this);
et2.setText(cde[i]);
tr[i].addView(et2);
EditText et3 = new EditText(this);
et3.setText(crd[i]);
tr[i].addView(et3);
ll.addView(tr[i]);
}
This is my code to create four edit text components in a row.
I need to give same text parameters to all the edittext components. But using methods to all four edittext components individually makes the code too lengthy.
Is there any solution so that I can use only one method to set text parameters for all the edittext components?
Using a method while adding your EditTexts to your TableRow can make your code easier to read.
private void addEditTextToTableRow(TableRow tableRow, String text) {
EditText editText = new EditText(this);
editText.setText(text);
tableRow.addView(editText);
}
Your for loop then becomes:
for (int i = 0; i < sub.length; i++) {
TableRow tr[i] = new TableRow(this);
addEditTextToTableRow(tr[i], message2);
addEditTextToTableRow(tr[i], sub[i]);
addEditTextToTableRow(tr[i], cde[i]);
addEditTextToTableRow(tr[i], crd[i]);
ll.addView(tr[i]);
}
try creating your components in the XML file, which will serve as a vision, and activity or in the XML Set The only text you need to set ... if you need utilzar one table, use the listView in XML and put all your EditText inside the listView

How to get EditText ID

I have added some EditTexts using a loop,
textFieldsLayout = (LinearLayout) findViewById(R.id.LinearLayout2);
for(int i=1; i <= 8; i++){
final EditText ed = new EditText(this);
ed.setText("" + i);
ed.setInputType(2);
ed.setLayoutParams(lparams);
textFieldsLayout.addView(ed);
}
}
After this I want to get the text that the user adds into the EditText fields, but I am stuck on how to do this. How would I get an Id for each of these EditText Fields?
Thanks, Oli
just add them to a collection when you're creating them that you can easily refer to by index or key or loop through or otherwise.
EditText[] etCollection = new EditText[8];
..........
textFieldsLayout = (LinearLayout) findViewById(R.id.LinearLayout2);
for(int i=1; i <= 8; i++){
final EditText ed = new EditText(this);
ed.setText("" + i);
ed.setInputType(2);
ed.setLayoutParams(lparams);
textFieldsLayout.addView(ed);
etCollection[i] = ed; <------ adding them to the collection
}
}
You'll have to do ed.setId( id ); on each one your create, and id should be unique for each one you want to find. When you go to find one do
EditText ed = (EditText)findViewById( id );
You'll have to keep track of your ids somewhere.
Alternatively you can just keep a list of the EditTexts that you create and run through the list to get the text of each one.

Categories

Resources