메인화면-옵션메뉴,isert버튼,목록
입력창-연필버튼을 누르면 입력창으로 이동
달력버튼을 누르면 달력이 나와서 날짜 변경 가능
옵션메뉴를 누르면
DiaryDB.java
package com.example.ex07;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class DiaryDB extends SQLiteOpenHelper {
public DiaryDB(@Nullable Context context) {
super(context, "diary.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table diary(_id integer primary key autoincrement, wdate text,subject text,content text)");
db.execSQL("insert into diary(wdate,subject,content) values('2019/11/11','안드로이드 난이도','안드로이드 쉬움')");
db.execSQL("insert into diary(wdate,subject,content) values('2019/11/10','android level','안드로이드 어려움')");
db.execSQL("insert into diary(wdate,subject,content) values('2019/11/9','Android version','안드로이드 보통')");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
InsertActivity.java
package com.example.ex07;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class InsertActivity extends AppCompatActivity {
DiaryDB helper;
SQLiteDatabase db;
EditText edtcontent,edtsubject;
TextView txtwdate;
int mYear,mMonth,mDay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insert);
edtcontent=findViewById(R.id.edtcontent);
edtsubject=findViewById(R.id.edtsubject);
txtwdate=findViewById(R.id.txtwdate);
helper=new DiaryDB(this);
db=helper.getWritableDatabase();
Calendar cal=new GregorianCalendar();
mYear=cal.get(Calendar.YEAR);
mMonth=cal.get(Calendar.MONTH);
//day에 따라서 month가 바뀜
mDay=cal.get(Calendar.DAY_OF_MONTH);
}
//button이 여러개 이므로 view를 받는다.
//모든 view(리젯)의 parent가 view
public void mClick(View v){
switch (v.getId()){
case R.id.btncal:
new DatePickerDialog(InsertActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
txtwdate.setText(String.format("%4d/%02d/%02d", year,month+1,day));
}
}, mYear, mMonth, mDay).show();
break;
case R.id.btncancel:
finish();
break;
case R.id.btnsave:
String strwdate=txtwdate.getText().toString();
String strsubject=edtsubject.getText().toString();
String strcontent=edtcontent.getText().toString();
String sql="insert into diary(wdate,subject,content) values(";
sql += "'" + strwdate +"',";
sql += "'" + strsubject + "',";
sql += "'" + strcontent + "')";
db.execSQL(sql);
finish();
break;
}
}
}
activity_insert.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".InsertActivity"
android:orientation="vertical"
android:padding="10sp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F44336"
android:padding="10sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2019/11/11"
android:textSize="20dp"
android:id="@+id/txtwdate"
android:textColor="#ffffff"
android:padding="10sp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_date_range_black_24dp"
android:id="@+id/btncal"
android:layout_alignParentRight="true"
android:onClick="mClick"/>
</RelativeLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint=" 제목입력"
android:textSize="20sp"
android:id="@+id/edtsubject"/>
<EditText
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:hint=" 내용입력"
android:textSize="20sp"
android:id="@+id/edtcontent"
android:gravity="top"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0px"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="저장"
android:textSize="20sp"
android:id="@+id/btnsave"
android:onClick="mClick"/>
<Button
android:layout_width="0px"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="취소"
android:textSize="20sp"
android:id="@+id/btncancel"
android:onClick="mClick"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.ex07;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class MainActivity extends AppCompatActivity {
DiaryDB helper;
SQLiteDatabase db;
MyAdapter adapter;
ListView list;
Cursor cursor;
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
int _id;
switch (item.getItemId()){
case 1:
Intent intent=new Intent(MainActivity.this,UpdateActivity.class);
intent.putExtra("_id",cursor.getInt(0));
startActivity(intent);
break;
case 2:
AlertDialog.Builder box=new AlertDialog.Builder(this);
box.setMessage("삭제 하시겠습니까?");
box.setNegativeButton("닫기",null);
box.setPositiveButton("확인", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String sql="delete from diary where _id=" + cursor.getInt(0);
db.execSQL(sql);
//refresh
onRestart();
}
});
box.show();
break;
}
return super.onContextItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle("메뉴선택" +cursor.getInt(0));
menu.add(0,1,0,"수정");
menu.add(0,2,0,"삭제");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
protected void onRestart() {
cursor=db.rawQuery("select * from diary order by wdate desc", null);
adapter.changeCursor(cursor);
super.onRestart();
}
//옵션메뉴 달기
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return super.onCreateOptionsMenu(menu);
}
//옵션메뉴 속성 지정
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.itemwdate:
onRestart();
break;
case R.id.itemsubject:
cursor=db.rawQuery("select * from diary order by subject desc",null);
adapter.changeCursor(cursor);
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper=new DiaryDB(this);
db=helper.getReadableDatabase();
//데이터 베이스 생성되었는지 확인
cursor=db.rawQuery("select * from diary order by wdate desc",null);
list=findViewById(R.id.list);
adapter=new MyAdapter(MainActivity.this,cursor);
list.setAdapter(adapter);
registerForContextMenu(list);
FloatingActionButton btnwrite=findViewById(R.id.btnwrite);
btnwrite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//intent에서 내가 이동할 activity 주소를 입력
Intent intent=new Intent(MainActivity.this,InsertActivity.class);
startActivity(intent);
}
});
}
class MyAdapter extends CursorAdapter{
public MyAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return getLayoutInflater().inflate(R.layout.item,parent,false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView txtwdate=view.findViewById(R.id.txtwdate);
txtwdate.setText(cursor.getString(1));
TextView txtsubject=view.findViewById(R.id.txtsubject);
txtsubject.setText(cursor.getString(2));
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list"
android:padding="10sp">
</ListView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/btnwrite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:srcCompat="@drawable/ic_create"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="20sp"
android:layout_marginBottom="20sp"
android:backgroundTint="#009688"/>
</RelativeLayout>
UpdateActivity.java
package com.example.ex07;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class UpdateActivity extends AppCompatActivity {
int _id;
DiaryDB helper;
SQLiteDatabase db;
Cursor cursor;
TextView txtwdate;
EditText edtsubject, edtcontent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insert);
txtwdate=findViewById(R.id.txtwdate);
edtsubject=findViewById(R.id.edtsubject);
edtcontent=findViewById(R.id.edtcontent);
Intent intent=getIntent();
_id=intent.getIntExtra("_id",0);
helper=new DiaryDB(this);
db=helper.getWritableDatabase();
cursor=db.rawQuery("select*from diary where _id="+_id, null);
if (cursor.moveToNext()){
txtwdate.setText(cursor.getString(1));
edtsubject.setText(cursor.getString(2));
edtcontent.setText(cursor.getString(3));
}
}
public void mClick(View v){
int mYear, mMonth, mDay;
switch (v.getId()){
case R.id.btncal:
String strwdate=txtwdate.getText().toString();
mYear = Integer.parseInt(strwdate.substring(0,4));
mMonth = Integer.parseInt(strwdate.substring(5,7));
mDay = Integer.parseInt(strwdate.substring(8,10));
new DatePickerDialog(UpdateActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
txtwdate.setText(String.format("%4d/%02d/%02d", year,month+1,day));
}
}, mYear, mMonth-1, mDay).show();
break;
case R.id.btnsave:
AlertDialog.Builder box=new AlertDialog.Builder(UpdateActivity.this);
box.setMessage("수정하시겠습니까?");
box.setPositiveButton("예", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String strwdate = txtwdate.getText().toString();
String strsubject=edtsubject.getText().toString();
String strcontent=edtcontent.getText().toString();
String sql="update diary set wdate='" +strwdate +"',";
sql += " subject='"+strsubject+"',";
sql += " content='" +strcontent+ "'";
sql +=" where _id=" +_id;
db.execSQL(sql);
finish();
}
});
box.setNegativeButton("아니오",null);
box.show();
break;
case R.id.btncancel:
}
}
}
activity_update.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".UpdateActivity">
</androidx.constraintlayout.widget.ConstraintLayout>
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/itemwdate"
android:title="날짜순 정렬"/>
<item
android:id="@+id/itemsubject"
android:title="제목순 정렬"/>
</menu>
'Android' 카테고리의 다른 글
Firebase를 이용한 이메일 로그인 (0) | 2019.11.12 |
---|---|
검색할 수 있는 메모장 만들기 (1) | 2019.11.12 |
주소록만들기(버튼,옵션메뉴) (0) | 2019.11.07 |
사칙연산 화면 만들기 (0) | 2019.11.07 |
메뉴와 서브메뉴만들기 (0) | 2019.11.07 |