Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中edittext按键事件的双时间数据插入_Android_Sqlite - Fatal编程技术网

Android中edittext按键事件的双时间数据插入

Android中edittext按键事件的双时间数据插入,android,sqlite,Android,Sqlite,当用户按下done/next/enter按钮时,我正在调用db-helper的方法,但该代码执行了两次,数据在表中输入了两次 以下是编辑文本的我的按键事件: editTextLitre.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) {

当用户按下done/next/enter按钮时,我正在调用db-helper的方法,但该代码执行了两次,数据在表中输入了两次

以下是编辑文本的我的按键事件:

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;
                }
            });

我不明白为什么数据会在数据库表中插入两次。

这很简单,按键过程中每个动作都会生成按键事件(即向下键和向上键)。您的代码检查键代码类型,但不验证事件是
ACTION\u DOWN
还是
ACTION\u UP

传入的
KeyEvent
对象为您提供此信息();您可能只想执行向上插入操作


由于您正在使用
EditText
,另一个可能更好的选项是
OnEditorActionListener
(),它不需要您检查这些内容。

将代码更改为:

    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;
                }
            }
        });
我更改的内容:
if(event.getAction()==KeyEvent.ACTION\u DOWN){…

因为当您按下一个键时,
onKey
事件会发生两次:按下时一次,松开键时一次