Click here to Skip to main content
15,885,868 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
i have a piece of code that decrements the number in a textView each time a number is entered into the editText. when i run the code the value is decremented twice instead of one. Like if i set the number to 5 after running the code i get the result 2 instead of 4.

Java
@Override
           public void afterTextChanged(Editable s) {

               String sScan = et.getText().toString();
               if (sScan.length() == 55 && sScan.contains("(01)") && sScan.contains("10") && sScan.contains("17") && sScan.contains("21"))
               {
                   int a = Integer.parseInt(tv.getText().toString());
                   Toast.makeText(getApplicationContext(), "good", Toast.LENGTH_LONG).show();
                   for(int i=1;i< a; i++)
                   {
                        a = a - i;
                       String val=Integer.toString(a);
                       tv.setText(val);
                       et.getText().clear();
                   }

               }
               else
               {
                   Toast.makeText(getApplicationContext(), "bad", Toast.LENGTH_LONG).show();
               }
Posted
Comments
Sergey Alexandrovich Kryukov 21-Jul-15 2:58am    
Debugger?
—SA
Rahul Ramakrishnan 21-Jul-15 3:18am    
figured it out.
a = Integer.parseInt(tv.getText().toString());
temp =a;
for(int i=1;i<=a;i++)
{
String sScan = et.getText().toString();
if (sScan.length() == 55 && sScan.contains("(01)") && sScan.contains("10") && sScan.contains("17") && sScan.contains("21"))
{
Toast.makeText(getApplicationContext(), "good", Toast.LENGTH_LONG).show();
temp=temp-1;
String val = Integer.toString(temp);
tv.setText(val);
et.setText("");
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900