按钮单击时重复插入值(重复)

时间:2017-08-11 10:29:17

标签: java android sqlite

我是android学习者的初学者,我有一个疑问,从日历视图中保存事件,当我提交我的事件时,为什么在提交后我的插入数据重复,这是我的代码,我附上我的屏幕截图输出,给我样本和想法。在按钮中调用该函数是错误的,如何访问它,为它提供解决方案。

enter image description here

btnEventSubmit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String getEvents = edtEvent.getText().toString();
                    if (getEvents.length() == 0) {
                        Toast.makeText(getApplicationContext(), "Please enter any events", Toast.LENGTH_LONG).show();
                    } else {
                        dialog.dismiss();
                        Toast.makeText(getApplicationContext(), "Your Event Submitted Successfully", Toast.LENGTH_LONG).show();
                        databasesqliteclass.createTabel();
                        databasesqliteclass.initialFunctionalities("insert into "
                                + Constants.TABLE_NAME + "(" + Constants.Column_1 + "," + Constants.Column_2 + "," + Constants.Column_3 + ","+Constants.Column_4+
                                ")values('" + getEvents + "','" + month + "','" + dayOfMonth + "'" + ",'" + year + "'"+ ")");
                        databasesqliteclass.selectQuery("Select *from " + Constants.TABLE_NAME);
                        databasesqliteclass.printTableData();
                        ArrayList<String> dataList = databasesqliteclass.printTableData1();
                        Log.i("dataList", "" + dataList);
                        for (int i = 0; i < dataList.size(); i++) {
                            Log.i("getsplit", "" + dataList.get(i));
                            String splitfrst = dataList.get(i);
                            String getsplitans[] = splitfrst.split("-");
                            String geteventss = null;
                            String geteventdate = null;
                            String geteventmonth = null;
                            String geteventyear=null;
                            Log.i("getsplitansgetsplitans", "" + getsplitans);
                            for (int j = 0; j < getsplitans.length; j++) {
                                geteventss = getsplitans[1];
                                geteventmonth = getsplitans[2];
                                geteventdate = getsplitans[3];
                                geteventyear=getsplitans[4];
                                Calendar cal = Calendar.getInstance();
                                int currentMonth = Integer.parseInt(geteventmonth);
                                SimpleDateFormat month_date = new SimpleDateFormat("MMMM");
                                cal.set(Calendar.MONTH, currentMonth);
                                String month_name = month_date.format(cal.getTime());
                                Log.i("month_name", "" + month_name);
                                Log.i("getevents", "" + geteventss);
                                Log.i("geteventmonth", "" + geteventmonth);
                                Log.i("geteventdate", "" + geteventdate);
                                eventsmodel = new Eventsmodel();
                                eventsmodel.setStrrevents(geteventss);
                                eventsmodel.setStrDate(geteventdate);
                                eventsmodel.setStrMonth(month_name);
                                eventsmodel.setStryear(geteventyear);
                            }
                            mCatagoryList.add(eventsmodel);
                        }


                        EventAdapter eventAdapter = new EventAdapter(getApplicationContext(), mCatagoryList);
                        Eventlist.setAdapter(eventAdapter);
                    }

                }
            });

这是我的选择查询,我在按钮单击中调用它以将值保存到数据库

public ArrayList<String> printTableData1() {
    SQLiteDatabase db = getReadableDatabase();
    Cursor cur = db.rawQuery("SELECT * FROM " + Constants.TABLE_NAME, null);
    if (cur.getCount() != 0) {
        cur.moveToFirst();
        if (cur.moveToFirst()) {
            data = new ArrayList<String>();
            do {
                row_values = "";
                for (int i = 0; i < cur.getColumnCount(); i++) {
                    row_values = row_values + "-" + cur.getString(i);
                }
                Log.d("LOG_TAG_HERE", row_values);
                data.add(row_values);
            } while (cur.moveToNext());
        }
    }
    db.close();
    return data;
}

0 个答案:

没有答案