Android Open Source - TodoList Add Activity






From Project

Back to project page TodoList.

License

The source code is released under:

Apache License

If you think the Android project TodoList listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package lyc.app;
//from   w ww.  ja v  a 2s .  c o m
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


public class AddActivity extends ActionBarActivity {

  private static final String TAG = "AddActivity";

  private String hour;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);

    final TextView title = (TextView) findViewById(R.id.titleTxtView);
    final TextView desc = (TextView) findViewById(R.id.descTxtView);
    final TextView planDate = (TextView) findViewById(R.id.planDateTxtView);

    Button btnSave = (Button) findViewById(R.id.btnSave);
    btnSave.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        //save data into db
        if (TextUtils.isEmpty(title.getText())) {
          Toast.makeText(AddActivity.this, "title required", Toast.LENGTH_SHORT).show();
          return;
        }

        if (TextUtils.isEmpty(planDate.getText())) {
          Toast.makeText(AddActivity.this, "plan finished date required", Toast.LENGTH_SHORT)
              .show();
          return;
        }

        if (!DateUtils.afterToday(planDate.getText().toString())) {
          Toast.makeText(AddActivity.this, "plan finished date error", Toast.LENGTH_SHORT)
              .show();
          return;
        }
        ContentValues cv = new ContentValues();
        cv.put(App.Todo.COLUMN_TITLE, title.getText().toString());
        cv.put(App.Todo.COLUMN_DESCRIPTION, desc.getText().toString());
        cv.put(App.Todo.COLUMN_PLAN_FINISHED_DATE, DateUtils.parse(planDate.getText().toString()));
        cv.put(App.Todo.COLUMN_CREATE_DATE, System.currentTimeMillis());
        cv.put(App.Todo.COLUMN_STATUS, String.valueOf(App.Todo.STATUS_UNFINISHED));
        getContentResolver().insert(App.Todo.CONTENT_URI, cv);
        setResult(RESULT_OK);
        finish();
      }
    });

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    View dialogView = getLayoutInflater().inflate(R.layout.date_hour_picker, null);
    final DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.datePicker);
    final Spinner spinner = (Spinner) dialogView.findViewById(R.id.hour_spinner);
    final ArrayAdapter<CharSequence>
        adapter =
        ArrayAdapter.createFromResource(this, R.array.hour_array,
                                        android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        hour = adapter.getItem(position).toString();
      }

      @Override
      public void onNothingSelected(AdapterView<?> parent) {

      }
    });

    builder.setView(dialogView);
    builder
        .setPositiveButton(R.string.button_positive_label, new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            Log.d(TAG, datePicker.getYear() + ", " + datePicker.getMonth() + ", " + datePicker
                .getDayOfMonth() + ", " + hour);
            planDate
                .setText(datePicker.getYear() + "-" + (datePicker.getMonth() + 1) + "-" + datePicker
                    .getDayOfMonth() + " " + hour + ":00:00");
          }
        });
    builder.setNegativeButton(R.string.button_negative_label, null);
    final AlertDialog dialog = builder.create();
    planDate.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        dialog.show();
        return false;
      }
    });
  }

}




Java Source Code List

lyc.app.AddActivity.java
lyc.app.App.java
lyc.app.BootReceiver.java
lyc.app.DateUtils.java
lyc.app.MainActivity.java
lyc.app.TodoService.java
lyc.app.adapter.TodoCursorAdapter.java
lyc.app.fragment.FinishedTodoFragment.java
lyc.app.fragment.UnFinishedTodoFragment.java
lyc.app.provider.TodoProvider.java
lyc.simplesqlite.annotation.Column.java
lyc.simplesqlite.annotation.Table.java
lyc.simplesqlite.exception.AnnotationNotFoundException.java
lyc.simplesqlite.util.MetaData.java
lyc.simplesqlite.util.StringUtils.java