vendredi 18 décembre 2015

Clear list when month has changed

In MainActivity, there has a textView, use to display current month, and a listView, use to load the data from SQLite.

WorkDetails

 btnSubmit.setOnClickListener(new View.OnClickListener() { // sumbit button is clicked
            public void onClick(View arg0) {
                first = objMyCustomBaseAdapter.getFistTime();
                String[] first1 = first.split(":", 2);
                last = objMyCustomBaseAdapter.getLastTime();
                String[] last1 = last.split(":", 2);
                a = ts.insertTimeSheet(name, weather, date2, status, first1[1], last1[1]);
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
            }
        });

When the submit button in WorkDetails is clicked, it will insert data into SQLite and intent to MainActivity.

MainActivity

In MainActivity, it will retrieved the data from SQLite and display on listView.

    Calendar cal=Calendar.getInstance();
    SimpleDateFormat month_date = new SimpleDateFormat("MMMM");
    String month_name = month_date.format(cal.getTime());
    Month.setText(month_name); // get current month
    BuildList(); // 


 public void BuildList()
    {
        sqlcon.open();
        Cursor cursor1=sqlcon.readData();

        String[] columns=new String[]{
        MyDatabaseHelper.Date,MyDatabaseHelper.TimeIn_Info,MyDatabaseHelper.TimeOut_Info
        };

        int[] to=new int[]
         {
           R.id.date,R.id.timeIn,R.id.timeOut
         };

        dataAdapter = new SimpleCursorAdapter(this, R.layout.retrieve_data,
                cursor1,
                columns,
                to,
                0);

        dataAdapter.setViewBinder(new ViewBinder());
        listView.setAdapter(dataAdapter);
    }

Image of MainActivity

enter image description here

How can I clear the listView when the month has changed ? Assume today is 1 January and I have 31 list in MainActivity. When the save button in WorkDetails is clicked, MainActivity listView will only display one list (December list item has been cleared) ?

Noted that the listView is clear, but data still have to stored in database. So the ID in SQLite should be 32, not 1.

Is it possible to achieve this ? Give me some hints please.

Aucun commentaire:

Enregistrer un commentaire