Generate number of Edit Text by storing andretrieve each value - android

I want to generate number of EditText column wise to store and retrieve each value .
Anyone can help me ?

I hope this may help you to resolve your problem.
EditText[] etxt = new EditText[Size];
EditText et;
//Generate and set values
for(int i=0;i<Size;i++)
{
et = new EditText(this);
et.setText(""+i)
et.setId(i);
etxt[i] = et;
//Add item to your view
//YourView.addView(etxt[i]);
}
Now you need to add to your View
//Get values
for(int s=0;s<etxt.lenght;s++)
{
etxt[s] = et;
Log.i("Test","Value: "+etxt[i].getText().toString());
}

Related

What's the best practice for keeping track of dynamically generated EditText?

I have a quantity of items which I'm retrieving from a database. The user is expected to enter a serial number for every item, and the input values are to be posted back to the database. Thus, a dynamic EditText is to be generated for every item. I normally do it this way:
int itemQuantity;
List<EditText> listEditText = new ArrayList<EditText>();
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
for(int i = 0; i < itemQuantity); i++){
EditText editText = new EditText(this);
editText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(editText);
listEditText.append(editText);
}
Then I use listEditText.get(index).getText().toString(); to retrieve values from each EditText.
Is this the right way of doing when considering performance and good practices?

how to List double values from dynamically added EditTexts android

I want to create a list of the double values. screen shot of my layout
When the (+) add button is pressed a minimum of three edit boxes are added.
i want to get the values of these edit boxes and be able to cross multiply them.
the values will be mostly +/- and with decimals.
How can i identify the edit boxes, then from the input Values, i set them in a List where i can be able to cross Multiply them.
I am trying this but i don't understand where am going wrong.
List<EditText> allNs = new ArrayList<EditText>();
List<EditText> allEs = new ArrayList<EditText>();
String[] northings = new String[allNs.size()];
String[] eastings = new String[allEs.size()];
double inputNorths, inputEasts = 0;
for(int i=0; i<allNs.size(); i++){
northings[i] = allNs.get(i).getText().toString();
inputNorths = Double.parseDouble(northings[i]);
northValues [1] = inputNorths;
}
resultN.add(northValues);
for(int e=0; e<allEs.size(); e++){
eastings[e] = allEs.get(e).getText().toString();
inputEasts = Double.parseDouble(eastings[e]);
eastValues[2] = inputEasts;
}
resultsE.add(eastValues);
calcArea();
To be honest I don't understand what are you doing here:
inputNorths = Double.parseDouble(northings[i]);
....
inputEasts = Double.parseDouble(eastings[e]);
Every loop you overwrite these variables. Maybe you forgot about adding?
inputNorths += Double.parseDouble(northings[i]);
northValues [1] += inputNorths;

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

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

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...

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