0

I am Beginner of the android learner, I have one doubt, Saving Event from Calendar view, when I was submit my event, why after submission my inserted data repeated, Here is my code, I attached my screenshot output, give me sample and idea. Is it wrong to call the function in button, how can access it, Give solution for it.

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

                }
            });

Here is my select query,i called it in button click for saving the values into database

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;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Selva Meena
  • 43
  • 10

1 Answers1

0

You need to declare and use your Eventsmodel outside the for loop. See below code.

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

eventsmodel = new Eventsmodel();
eventsmodel.setStrrevents(geteventss);
eventsmodel.setStrDate(geteventdate);
eventsmodel.setStrMonth(month_name);
eventsmodel.setStryear(geteventyear);
UmarZaii
  • 1,355
  • 1
  • 17
  • 26