user3526192
user3526192

Reputation: 11

Data inserted double time on key press event of edittext in Android

I am calling method of db helper while user press done/next/enter, button but that code is executed twice and data entered twice in table.

Here is my key press event of edit text:

editTextLitre.setOnKeyListener(new OnKeyListener() {

                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    // TODO Auto-generated method stub
                    switch(keyCode){
                    case KeyEvent.KEYCODE_ENTER:
                        TextView txtlitres = (TextView)findViewById(R.id.edit_text_litre);
                        TextView totalamount = (TextView)findViewById(R.id.amount);

                        RadioButton rbBuffalo = (RadioButton) findViewById(R.id.radioBuffalo);

                        RadioButton rbCow = (RadioButton) findViewById(R.id.radioCow);
                        rbBuffalo.setText(Constants.Buffalo.toString());
                        rbCow.setText(Constants.Cow.toString());
                        radioanimal= (RadioGroup)findViewById(R.id.radioCategory);
                        int selectedid = radioanimal.getCheckedRadioButtonId();

                        rb = (RadioButton)findViewById(selectedid);


                        DBHelper dh=new DBHelper(getApplicationContext());
                        // TODO Auto-generated method stub
                        boolean save = dh.insertMilkDetails(mandali,rb.getText().toString(),totalamount.getText().toString(),txtlitres.getText().toString(),dt.toString(),session.toString());

                        if(save == true)
                        {
                             Toast.makeText(getApplicationContext(), "Details Saved Successfully", Toast.LENGTH_SHORT).show();

                        }

                    return false;
                }
            });

I can't understand why data is inserted twice in database table.

Upvotes: 0

Views: 163

Answers (2)

Sebastien Bianchi
Sebastien Bianchi

Reputation: 722

Change your code to this:

    editTextLitre.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_ENTER:
                        TextView txtlitres = (TextView) findViewById(R.id.edit_text_litre);
                        TextView totalamount = (TextView) findViewById(R.id.amount);

                        RadioButton rbBuffalo = (RadioButton) findViewById(R.id.radioBuffalo);

                        RadioButton rbCow = (RadioButton) findViewById(R.id.radioCow);
                        rbBuffalo.setText(Constants.Buffalo.toString());
                        rbCow.setText(Constants.Cow.toString());
                        radioanimal = (RadioGroup) findViewById(R.id.radioCategory);
                        int selectedid = radioanimal.getCheckedRadioButtonId();

                        rb = (RadioButton) findViewById(selectedid);


                        DBHelper dh = new DBHelper(getApplicationContext());
                        // TODO Auto-generated method stub
                        boolean save = dh.insertMilkDetails(mandali, rb.getText().toString(), totalamount.getText().toString(), txtlitres.getText().toString(), dt.toString(), session.toString());

                        if (save == true) {
                            Toast.makeText(getApplicationContext(), "Details Saved Successfully", Toast.LENGTH_SHORT).show();

                        }

                        return false;
                }
            }
        });

What I changed : if (event.getAction() == KeyEvent.ACTION_DOWN) { ...

Because an onKey event happens twice when you press a key: Once when you press, once when you release the key.

Upvotes: 0

devunwired
devunwired

Reputation: 63303

It's quite simple, key events are generated for every action during a key press (i.e key down AND key up). Your code checks for the key code type, but does not validate whether the event was ACTION_DOWN or ACTION_UP.

The KeyEvent object passed in provides you this information (see docs); you likely only want to do the insert on the up action.

Another, probably better, option since you are using EditText is an OnEditorActionListener (docs), which doesn't require you to check such things.

Upvotes: 2

Related Questions