Open emmalady95 opened 6 years ago
package com.example.sev_user.todolist.activity;
import android.app.AlarmManager; import android.app.AlertDialog; import android.app.DatePickerDialog; import android.app.TimePickerDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent;
import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore;
import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText;
import android.widget.LinearLayout; import android.widget.Spinner; import android.widget.TimePicker; import android.widget.Toast;
import com.example.sev_user.todolist.R; import com.example.sev_user.todolist.adapter.PhotoAdapter;
import com.example.sev_user.todolist.database.ConnectDatabase; import com.example.sev_user.todolist.database.DatabaseManager; import com.example.sev_user.todolist.model.Notes; import com.example.sev_user.todolist.utils.DatabaseUtils; import com.example.sev_user.todolist.utils.DateFormatUtils; import com.example.sev_user.todolist.utils.KeyUtils;
import java.util.ArrayList;
import java.util.Calendar; import java.util.Date; import java.util.List;
public class AddNoteActivity extends AppCompatActivity { private AlertDialog mPhotoDialog;
private EditText edtTitle; private EditText edtContent; private EditText edtPlace; private Spinner spCategory; private Button btnAlarm; private EditText spDateBegin; private EditText spTimeBegin; private EditText spDateEnd; private EditText spTimeEnd; private RecyclerView rcPhoto; private ActionBar mActionBar; private Uri image; private Date mAlarm; public AlarmManager mAlarmManager; private PhotoAdapter mPhotoAdapter; private ArrayAdapter<String> spAdapter; public List<String> mDateAlarmList = new ArrayList<>(); public List<String> mTimeAlarmList = new ArrayList<>(); public List<String> mPhotoList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_note); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); initViews(); setSupportActionBar(toolbar); customActionbar(); initViews(); loadSpinnerCategory(); setListViewAdapter(); catchOnClickEvent(); } private void loadSpinnerCategory() { List<String> category = new ArrayList<>(); category = DatabaseManager.getInstance(AddNoteActivity.this).getAllCategory(); spAdapter = new ArrayAdapter(AddNoteActivity.this, android.R.layout.simple_spinner_item, category); spAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spCategory.setAdapter(spAdapter); } private void setListViewAdapter() { mPhotoAdapter = new PhotoAdapter(mPhotoList, AddNoteActivity.this); rcPhoto.setAdapter(mPhotoAdapter); LinearLayoutManager linearLayout = new LinearLayoutManager(AddNoteActivity.this); linearLayout.setOrientation(LinearLayout.HORIZONTAL); rcPhoto.setLayoutManager(linearLayout); } private void catchOnClickEvent() { btnAlarm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(btnAlarm.getText().toString().equals("Tắt")){ btnAlarm.setText(R.string.btn_on_alarm); }else{ btnAlarm.setText(R.string.btn_off_alarm); } } }); spDateBegin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showDateDialog(spDateBegin); } }); spTimeBegin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showTimeDialog(spTimeBegin); } }); spTimeBegin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showTimeDialog(spTimeBegin); } }); spDateEnd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showDateDialog(spDateEnd); } }); spTimeEnd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showTimeDialog(spTimeEnd); } }); } private void customActionbar() { this.mActionBar = getSupportActionBar(); this.mActionBar.setDisplayOptions(16); this.mActionBar.setCustomView((int) R.layout.custom_actionbar); this.mActionBar.setDisplayShowHomeEnabled(true); this.mActionBar.setTitle(getString(R.string.app_name)); } private void initViews() { rcPhoto = (RecyclerView) findViewById(R.id.rc_add_photos); edtContent = (EditText) findViewById(R.id.edt_detail); edtTitle = (EditText) findViewById(R.id.edt_title); spCategory = (Spinner) findViewById(R.id.sp_category); btnAlarm = (Button) findViewById(R.id.btn_enabled_alarm); spDateBegin = (EditText) findViewById(R.id.sp_date_begin); spTimeBegin = (EditText) findViewById(R.id.sp_time_begin); spDateEnd = (EditText) findViewById(R.id.sp_date_end); spTimeEnd = (EditText) findViewById(R.id.sp_time_end); edtPlace = (EditText) findViewById(R.id.edt_place); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_add, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); switch (id){ case R.id.action_camera:
// Toast.makeText(MainActivity.this, "Search", Toast.LENGTH_SHORT).show(); showPhotoDialog(); return true; case R.id.action_complete: String title = edtTitle.getText().toString(); String alarm = btnAlarm.getText().toString(); String timeBegin = spTimeBegin.getText().toString(); String dateBegin = spDateBegin.getText().toString(); String timeEnd = spTimeEnd.getText().toString(); String dateEnd = spDateEnd.getText().toString(); String category = spCategory.getSelectedItem().toString(); String place = edtPlace.getText().toString(); String content = edtContent.getText().toString();
String query = DatabaseUtils.QUERY_SELECT_ID_CATEGORY+" WHERE "+DatabaseUtils.CATEGORY_COLUMN_NAME+" = '"+category+"'"; SQLiteDatabase db = ConnectDatabase.sqLiteDatabase(AddNoteActivity.this, DatabaseUtils.DATABASE_NAME); Cursor cursor = db.rawQuery(query, null); cursor.moveToFirst(); int idCategory = cursor.getInt(0); if(title.equals("")){ Toast.makeText(AddNoteActivity.this, "Bạn chưa nhập tiêu đề", Toast.LENGTH_SHORT).show(); }else if(timeBegin.equals("")) { Toast.makeText(AddNoteActivity.this, "Bạn chưa nhập thời gian bắt đầu", Toast.LENGTH_SHORT).show(); }else if(timeEnd.equals("")) { Toast.makeText(AddNoteActivity.this, "Bạn chưa nhập thời gian kết thúc", Toast.LENGTH_SHORT).show(); }else if(dateBegin.equals("")){ Toast.makeText(AddNoteActivity.this, "Bạn chưa nhập ngày bắt đầu", Toast.LENGTH_SHORT).show(); }else if(dateEnd.equals("")) { Toast.makeText(AddNoteActivity.this, "Bạn chưa nhập ngày kết thúc", Toast.LENGTH_SHORT).show(); }else { int baothuc = 0; Notes notes = new Notes(); if(alarm.equals("Bật")){ baothuc = 1; } //String mTitle, String mContent, Date mTimeBegin, Date mTimeEnd, int mCategory, Boolean mAlarm, String mPlace; Notes note = new Notes(title, content, dateBegin+" "+timeBegin, dateEnd+" "+timeEnd,idCategory, baothuc, place); DatabaseManager.getInstance(AddNoteActivity.this).insertNotesData(note); //if(kq == 1){ //Toast.makeText(AddNoteActivity.this, kq, Toast.LENGTH_LONG).show(); //finish(); //}else{ // Toast.makeText(AddNoteActivity.this, "KHONG THANH CONG", Toast.LENGTH_LONG).show(); //} //Toast.makeText(AddNoteActivity.this, "CHUOI INSERT la: "+title+" - "+content+" - "+ dateBegin+" "+timeBegin+" - "+dateEnd+" "+timeEnd+ " - "+ idCategory+ " - "+ baothuc+" - "+place,Toast.LENGTH_SHORT).show(); /*long kq = DatabaseManager.getInstance(AddNoteActivity.this).insertNotesData(note); if(kq == 1){)*/ } return true; case R.id.action_cancel: finish(); return true; } return super.onOptionsItemSelected(item); } /* *DIALOG */ private void showPhotoDialog() { this.mPhotoDialog = new AlertDialog.Builder(this).create(); this.mPhotoDialog.setView(((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)) .inflate(R.layout.dialog_insert_photo, null)); this.mPhotoDialog.show(); } public void takeCamera(View v){ mPhotoDialog.dismiss(); Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getPackageManager()) != null) { startActivityForResult(takePictureIntent, KeyUtils.REQUEST_PICK_CAMERA); } } public void takeGalery(View v){ mPhotoDialog.dismiss(); Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Select Picture"), KeyUtils.REQUEST_PICK_GALLERY); } public void showDateDialog(final EditText edt){ final Date date = mAlarm; String chooseDate = "Chọn ngày"; Calendar calendar = Calendar.getInstance(); final DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { String d = ""; String m = ""; if(dayOfMonth < 10){ d = "0" + String.valueOf(dayOfMonth); }else { d = String.valueOf(dayOfMonth); } if(month < 10){ m = "0" + String.valueOf(month+1); }else { m = String.valueOf(month+1); } edt.setText(new StringBuilder().append(d).append("/").append(m).append("/").append(year)); } }, calendar.get(calendar.YEAR), calendar.get(calendar.MONTH), calendar.get(calendar.DATE)); datePickerDialog.setTitle(chooseDate); datePickerDialog.setCancelable(true); datePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { public void onCancel(DialogInterface dialog) { datePickerDialog.dismiss(); } }); datePickerDialog.show(); } public void showTimeDialog(final EditText edt) { String chooseTime = "Chọn thời gian"; Calendar calendar = Calendar.getInstance(); final TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { String h = ""; String m = ""; if(hourOfDay < 10){ h = "0" + String.valueOf(hourOfDay); }else { h = String.valueOf(hourOfDay); } if(minute < 10){ m = "0" + String.valueOf(minute); }else { m = String.valueOf(minute); } edt.setText(new StringBuilder().append(h).append(":").append(m)); } }, calendar.get(calendar.HOUR_OF_DAY) + 1, 0, true); timePickerDialog.setTitle(chooseTime); timePickerDialog.setCancelable(true); timePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { public void onCancel(DialogInterface dialog) { timePickerDialog.dismiss(); } }); timePickerDialog.show(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == KeyUtils.REQUEST_PICK_CAMERA){ if(resultCode == RESULT_OK){ Bundle bundle = data.getExtras(); //image = data.getData(); //mPhotoList.add((String) bundle.get("data")); // Bitmap imageBitmap = (Bitmap) bundle.get("data"); //mImageView.setImageBitmap(imageBitmap); } }else if(requestCode == KeyUtils.REQUEST_PICK_GALLERY){ if(resultCode == RESULT_OK && data != null && data.getData() != null) { image = data.getData(); mPhotoList.add(image.toString()); } } super.onActivityResult(requestCode, resultCode, data); }
}
package com.example.sev_user.todolist.activity;
import android.app.AlarmManager; import android.app.AlertDialog; import android.app.DatePickerDialog; import android.app.TimePickerDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent;
import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore;
import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText;
import android.widget.LinearLayout; import android.widget.Spinner; import android.widget.TimePicker; import android.widget.Toast;
import com.example.sev_user.todolist.R; import com.example.sev_user.todolist.adapter.PhotoAdapter;
import com.example.sev_user.todolist.database.ConnectDatabase; import com.example.sev_user.todolist.database.DatabaseManager; import com.example.sev_user.todolist.model.Notes; import com.example.sev_user.todolist.utils.DatabaseUtils; import com.example.sev_user.todolist.utils.DateFormatUtils; import com.example.sev_user.todolist.utils.KeyUtils;
import java.util.ArrayList;
import java.util.Calendar; import java.util.Date; import java.util.List;
public class AddNoteActivity extends AppCompatActivity { private AlertDialog mPhotoDialog;
// Toast.makeText(MainActivity.this, "Search", Toast.LENGTH_SHORT).show(); showPhotoDialog(); return true; case R.id.action_complete: String title = edtTitle.getText().toString(); String alarm = btnAlarm.getText().toString(); String timeBegin = spTimeBegin.getText().toString(); String dateBegin = spDateBegin.getText().toString(); String timeEnd = spTimeEnd.getText().toString(); String dateEnd = spDateEnd.getText().toString(); String category = spCategory.getSelectedItem().toString(); String place = edtPlace.getText().toString(); String content = edtContent.getText().toString();
}