2

I have to add values to my sq lite from the list view. In my list view there are two edit texts and text view. I just want to get value from each edit text and multiply it to the corresponding text view value. when I am running the app, I need not to enter data to every edit text. due to this I am ending with a "number format exception : invalid int". From other examples I can understand that multiplication on null value may cause the error. how can I skip the null value contained edit texts from the iteration? this is my code

protected void InsertDb() {
// TODO Auto-generated method stub
 DatabaseHelper databasecontroller = new DatabaseHelper(Orders.this);
 List<Map<String, String>> data = null;
 data = new ArrayList<Map<String, String>>();

   if(list != null){

        for(int i = 0; i< list.getChildCount();i++){

            View vie = list.getChildAt(i);

            EditText ed1= (EditText) vie.findViewById(R.id.cases);
            EditText ed2 = (EditText) vie.findViewById(R.id.pcs);
            String qty = ed1.getText().toString();

           TextView tv = (TextView)findViewById(R.id.srp);
           String imsrpv= tv.getText().toString();

            float srps = Float.valueOf(imsrpv);
            int qtys = Integer.valueOf(qty);
           float amts = srps * qtys; 
           String amount = Float.toString(amts);

           datanum.put("A",qty );
            datanum.put("B",ed2.getText().toString() );
           datanum.put("L", amount);

            Log.d("value of amnt",amount);

            databasecontroller.entercustdetails(datanum); 
        }

           }
   Log.v("compleated", data.toString());
}

Thanks in advance..

Bivin
  • 375
  • 3
  • 24
  • http://stackoverflow.com/questions/10735679/how-to-convert-string-into-float-value-in-android – saeed Mar 16 '16 at 06:25
  • If I am understanding You the right way, You have not a problem parsing a valid string to float if the string consists of number/float values, wright? You only want to be sure the value is not null, aren´t you? – Opiatefuchs Mar 16 '16 at 06:36
  • almost. I have alternate empty values in the list.so when i am trying to iterate over the list i just want to skip all the unfilled edit texts in between them.is it possible? – Bivin Mar 16 '16 at 06:45
  • yes, see my answer.... – Opiatefuchs Mar 16 '16 at 07:01

2 Answers2

2

Check either returning value from edittext is a proper number or it is not null.if a null value is there it may give exeception of numberformat.you have to handle this. if both are correct then you can also you

float srps=0.0;
int qtys=0;
 try
{
  srps = Float.valueOf(imsrpv);
  qtys = Integer.valueOf(qty);
}
catch (NumberFormatException e)
{
srps =//Default value you want;
qtys =//Default value you want;
}

Hope it works..

Ankur1994a
  • 2,112
  • 2
  • 13
  • 18
  • 1
    when I am using both the methods I got error.My edit text resides in a list view, and the list is little large.I don't want to enter values at each edit texts.I want to perform multiplication only on the entered fields. – Bivin Mar 16 '16 at 06:43
  • if you leave blank a edittext it give a empty string and it gives a NumberFormatException. you have handle this exception. as i edit my answer. – Ankur1994a Mar 16 '16 at 06:53
  • thank you, but my question is how to skip blank edit texts from the list. I tried various loops but i am getting only first value as calculated. – Bivin Mar 16 '16 at 07:32
  • in this if you skip blank edit text it automatically handle. only you have to give default value if edit text is empty in catch block. – Ankur1994a Mar 16 '16 at 07:40
  • yes. It gives the calculation of same list values..thank you – Bivin Mar 16 '16 at 07:49
1

Your problem is not how You have parsed the values. For explanation:

parseFloat(), parseInt() will return a primitive type of int and float and valueOf() returns a new type of Integer and Float. Also, parseInt(), parseFloat() will recognize plus and minus signs, valueOf() doesn´t. That´s the difference between these two types/methods. Your problem seems to be, that the value is empty and You can simply get rid of this by:

//set a default float and int

float srps = 0.0;
int qtys = 0;

//now parse the values by checking if the strings are not null or empty
if(imsrpv!=null&&!imsrpv.isEmpty()){

srps = Float.valueOf(imsrpv);

} 

if(qty!=null&&!qty.isEmpty()){

qtys = Integer.valueOf(qty);
}

Like I said, if these values have a plus or minus sign and it is important for Your needs, You should use the parse method. Also, You don´t need a new Integer or Float type, so more correct is using parse() method.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
  • Hi this method really worked for me. But still i have a problem that when I am entering the value and scrolls down, the same value automatically appears for other entries. and the calculation takes place on some other value.do you have any idea? – Bivin Mar 16 '16 at 07:27
  • ok, but that´s a completely other problem. I think, the problem You have asked for here is solved by the answer. The problem You have now described is that ListViews recycle their content as soon as the content is invisible. For that, You have to use Viewholder pattern and should make a new question. See here for viewholder pattern: https://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html – Opiatefuchs Mar 16 '16 at 07:38