0
votes

I have this code to detect number format from my EditText input.

public class NumberTextWatcher implements TextWatcher {

    private DecimalFormat df;
    private DecimalFormat dfnd;
    private boolean hasFractionalPart;

    private EditText et;

    public NumberTextWatcher(EditText et)
    {
        df = new DecimalFormat("#,###.##");
        df.setDecimalSeparatorAlwaysShown(true);
        dfnd = new DecimalFormat("#,###");
        this.et = et;
        hasFractionalPart = false;
    }

    @SuppressWarnings("unused")
    private static final String TAG = "NumberTextWatcher";

    @Override
    public void afterTextChanged(Editable s)
    {
        et.removeTextChangedListener(this);

        try {
            int inilen, endlen;
            inilen = et.getText().length();

            String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "");
            Number n = df.parse(v);
            int cp = et.getSelectionStart();
            if (hasFractionalPart) {
                et.setText(df.format(n));
            } else {
                et.setText(dfnd.format(n));
            }
            endlen = et.getText().length();
            int sel = (cp + (endlen - inilen));
            if (sel > 0 && sel <= et.getText().length()) {
                et.setSelection(sel);
            } else {
                // place cursor at the end?
                et.setSelection(et.getText().length() - 1);
            }
        } catch (NumberFormatException nfe) {
            // do nothing?
        } catch (ParseException e) {
            // do nothing?
        }

        et.addTextChangedListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after)
    {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count)
    {
        if (s.toString().contains(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator())))
        {
            hasFractionalPart = true;
        } else {
            hasFractionalPart = false;
        }
    }

}

And then call it in activity.

JumlahTrx = (EditText)findViewById(R.id.editJumlah);
JumlahTrx.addTextChangedListener(new NumberTextWatcher(JumlahTrx));

And it will produce 5,000 if I input the field. What I want is when I click on button the value 5,000 become 5000 and then insert to my database. Because I set on INT in my database.

Any help will be nice. Thanks

====================== UPDATE

add this code

String data = JumlahTrx.getText().toString();
data=data.replaceAll(",","");
int value=Integer.parseInt(data);
JumlahTrx.setText(value);

and got this error when on button click to save it in database

11-26 20:27:54.441 2609-2609/com.androidjson.serverupdate_androidjsoncom E/AndroidRuntime: FATAL EXCEPTION: main Process: com.androidjson.serverupdate_androidjsoncom, PID: 2609 android.content.res.Resources$NotFoundException: String resource ID #0x61a8 at android.content.res.Resources.getText(Resources.java:335) at android.widget.TextView.setText(TextView.java:4555) at com.androidjson.serverupdate_androidjsoncom.MainActivity.StudentRegistration(MainActivity.java:120) at com.androidjson.serverupdate_androidjsoncom.MainActivity$1.onClick(MainActivity.java:53) at android.view.View.performClick(View.java:5657) at android.view.View$PerformClick.run(View.java:22453) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6236) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)

==============UPDATE 2====================

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    JenisTrx = (EditText)findViewById(R.id.editJenis);
    KeteranganTrx = (EditText)findViewById(R.id.editKeterangan);
    JumlahTrx = (EditText)findViewById(R.id.editJumlah);
    JumlahTrx.addTextChangedListener(new NumberTextWatcher(JumlahTrx));


    RegisterStudent = (Button)findViewById(R.id.buttonSubmit);
    ShowStudents = (Button)findViewById(R.id.buttonShow);

    RegisterStudent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            // Checking whether EditText is Empty or Not
            CheckEditTextIsEmptyOrNot();

            if(CheckEditText){

                // If EditText is not empty and CheckEditText = True then this block will execute.

                StudentRegistration(JenisTrxHolder,KeteranganTrxHolder, JumlahTrxHolder);

            }
            else {

                // If EditText is empty then this block will execute .
                Toast.makeText(MainActivity.this, "Please fill all form fields.", Toast.LENGTH_LONG).show();

            }

        }
    });

    ShowStudents.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            Intent intent = new Intent(getApplicationContext(),ShowAllStudentsActivity.class);

            startActivity(intent);

        }
    });
 }

public void StudentRegistration(final String S_Jenis, final String S_Keterangan, final String S_Jumlah){

    class StudentRegistrationClass extends AsyncTask<String,Void,String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            progressDialog = ProgressDialog.show(MainActivity.this,"Loading Data",null,true,true);
        }

        @Override
        protected void onPostExecute(String httpResponseMsg) {

            super.onPostExecute(httpResponseMsg);
            String a = JumlahTrx.getText().toString();
            a=a.replaceAll(",","");
            JumlahTrx.setText(a);
            System.out.println( a );
            progressDialog.dismiss();

            Toast.makeText(MainActivity.this,httpResponseMsg.toString(), Toast.LENGTH_LONG).show();

        }

        @Override
        protected String doInBackground(String... params) {

            hashMap.put("JenisTrx",params[0]);

            hashMap.put("KeteranganTrx",params[1]);

            hashMap.put("JumlahTrx",params[2]);

            finalResult = httpParse.postRequest(hashMap, HttpURL);

            return finalResult;
        }
    }

    StudentRegistrationClass studentRegistrationClass = new StudentRegistrationClass();

    studentRegistrationClass.execute(S_Jenis,S_Keterangan,S_Jumlah);
}

public void CheckEditTextIsEmptyOrNot(){

    JenisTrxHolder = JenisTrx.getText().toString();
    KeteranganTrxHolder = KeteranganTrx.getText().toString();
    JumlahTrxHolder = JumlahTrx.getText().toString();

    if(TextUtils.isEmpty(JenisTrxHolder) || TextUtils.isEmpty(KeteranganTrxHolder) || TextUtils.isEmpty(JumlahTrxHolder))
    {

        CheckEditText = false;

    }
    else {

        CheckEditText = true ;
    }

}
2
Error you're getting is entirely justified. It's invalid to call TextView::setText with arbitrary integer values. Please read what that method actually does and expects.M. Prokhorov

2 Answers

1
votes

You may use replace or replaceAll function before saving data to database For Example

String data = "5,000";
data= data.replaceAll(",","");

Int value = Integer.parseInt(data);

Store that value to your data base

Updated

in your doInBackGround

Change

hashMap.put("JumlahTrx",params[2]);

To

 String a = params[2];
 a=a.replaceAll(",","");
 hashMap.put("JumlahTrx",a);
0
votes

As you said What I want is when I click on button the value 5,000 become 5000. If this is only the requirement then i have an alternate solution. Try this

String a="5,00,000";

a=a.replaceAll(",","");

int num=Integer.parseInt(a);