getting integer value entered from dynamically created edittext in a dialog in Android throws NumberFormatException

StackOverflow https://stackoverflow.com/questions/8616826

Question

I'm new, so sorry if this turns out so simple that I should have solved myself. I've spent a couple days thinking about it. I've researched a ton and searched many other posts here, but no success.

I have a dialog with DatePicker, Buttons, EditText fields, and Spinners. I can populate everything i need from the stored items in my DB to the dialog. But when i try to get the numbers entered into some of the EditText fields, it throws NumberFormatException. I can hard code values into variables to store, and hide my attempt to get the value in a try and it will run fine. The values get stored and when I open the dialog again they populate into the right areas.

Here's partial code-

        EditText et = new EditText(getContext()); 
        for(int i=0; i<=etcount; i++){
        placed = -1; //reset for next iteration

        //try to get number from edittext
        et.findViewById(i);
        placed = Integer.parseInt(et.getText().toString());

        awake++;
        /*
        try {
            //et.findViewById(i);
            placed = Integer.parseInt(findViewById(i).toString());
            //placed = Integer.parseInt(et.getText().toString());
        } catch(NumberFormatException nfe) {
           System.out.println("Could not parse " + nfe);
        }*/

I commented out the try block cause I wanted to troubleshoot quickly.

etcount variable is initialized at onCreate with -1 value, before getting values from DB. If there are values stored in the DB it gets incremented by 1, then code is called to dynamically add an EditText and Spinner to the layout. Also the EditText id is set to the value of etcount. Here is that code-

//this will be ran when +placement button pressed, or if there are items in db stored
//adds 2 rows to dialog, one for textview to label items, one row for edittext with number
//and spinner with what item it is
private void createTableRow(int numPlaced, int itmPlaced){ 


    etcount++; //used to count how many edittext fields there are so that they can be saved later
    spincount++; //to count how many spinner there are


    tl = (TableLayout)findViewById(R.id.tableLayoutVisit); //the tablelayout name
    //need to create row for textview, then another row for edittext and spinner
    tr1 = new TableRow(this.getContext()); //table row 1, for textview
    tr1.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
    TextView tv = new TextView(this.getContext());
    tv.setText("Amount Placed");
    tv.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));

    tr1.addView(tv); //add textview to tablerow1
    tl.addView(tr1, new TableLayout.LayoutParams( //add row to tablelayout
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));


    tr2 = new TableRow(this.getContext()); //tablerow2: edittext and spinner
    tr2.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));        
    EditText et = new EditText(this.getContext());
    et.setId(etcount);
    et.setInputType(InputType.TYPE_CLASS_NUMBER);
    if(numPlaced!=0){ //if being populated from previous visit  
        et.setText(Integer.toString(numPlaced));
        //et.setText("" +numPlaced);
    }

    //need to have listener to read data
    et.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));


    Spinner spin = new Spinner(this.getContext());
    spin.setId(spincount);
    spinArray = getContext().getResources().getStringArray(R.array.itemplaced);
    ArrayAdapter adapter = new ArrayAdapter(getContext(),
            android.R.layout.simple_spinner_item, spinArray);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


    spin.setAdapter(adapter);
    if(itmPlaced!=-1){
        spin.setSelection(itmPlaced); //assign correct value 
    }
    spin.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
    tr2.addView(et);
    tr2.addView(spin);


    tl.addView(tr2, new TableLayout.LayoutParams( //add row to tablelayout
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));

}

I pass it (0,-1) if it is a new item and not currently stored in DB. Also you probably noticed I forced it to accept TYPE_CLASS_NUMBER. But I don't think the issue is there. Again, if I hard code values it will save into the DB and the next time I open it it dynamically creates EditText/Spinner row in layout and populates the EditText with the value in the DB.

So... something is wrong with the first section I think, the et.findViewById(i); or placed = Integer.parseInt(et.getText().toString());. The value of "etcount" should be -1 if nothing was populated from DB, and 0 etc(etcount++) each time something is populated from DB.

Sorry if this is longwinded, wanted to be specific. Hope this is enough info! Any help would be great!!! Thanks in advance.

Was it helpful?

Solution

Try this:

    EditText et = new EditText(this);

    for (int i = 0; i <= etcount; i++) {
        placed = -1;
        et = (EditText) this.findViewById(i);
        placed = Integer.parseInt(et.getText().toString());
        //...
    }

It seems to me that I've found an error. The main problem in your code that you do not assign unique values in setId methods. In your case some editTexts and spins can have the same IDs. This is wrong. Every element must have a unique identifier. This is the first what you should correct in your code.

After that you can use my approach. But it seems to me that something wrong in this approach. It is not beautiful ) Try to find best practices in this field.

OTHER TIPS

Few suggestions from my side: 1) try to check whether getText method not giving you empty string"" or null. 2) place your EditText et; to class variable not a local instance. example public class ExampleActivity extends Activity { EditText et;//place it here instead in oncreate methods.

}// or make it final if defining in oncreate.

according to your code, issues seems in your for loop for(int i=0; i<=etcount; i++){ placed = -1; //reset for next iteration

    //try to get number from edittext  // which edit text you are trying to access. am sure it is  returning null to you.
    et.findViewById(i);
    placed = Integer.parseInt(et.getText().toString());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top