diff --git a/.idea/2017-basic-work3-chapter08.iml b/.idea/2017-basic-work3-chapter08.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/2017-basic-work3-chapter08.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..c2bae49 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..3f08c7c --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..4b27f68 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..0b938b6 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,396 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1501805998069 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/MyScheduler/.idea/misc.xml b/MyScheduler/.idea/misc.xml index 1d9d626..5d19981 100644 --- a/MyScheduler/.idea/misc.xml +++ b/MyScheduler/.idea/misc.xml @@ -1,5 +1,32 @@ + + + + + + + diff --git a/MyScheduler/.idea/vcs.xml b/MyScheduler/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/MyScheduler/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/MyScheduler/app/build.gradle b/MyScheduler/app/build.gradle index ad58b8b..702dcd5 100644 --- a/MyScheduler/app/build.gradle +++ b/MyScheduler/app/build.gradle @@ -1,4 +1,5 @@ apply plugin: 'com.android.application' +apply plugin: 'realm-android' android { compileSdkVersion 26 @@ -27,5 +28,6 @@ dependencies { compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.android.support:design:26.+' + compile 'io.realm:android-adapters:2.0.0' testCompile 'junit:junit:4.12' } diff --git a/MyScheduler/app/src/main/AndroidManifest.xml b/MyScheduler/app/src/main/AndroidManifest.xml index 3679872..a9c568d 100644 --- a/MyScheduler/app/src/main/AndroidManifest.xml +++ b/MyScheduler/app/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ package="com.example.yu_enpit.myscheduler"> + \ No newline at end of file diff --git a/MyScheduler/app/src/main/java/com/example/yu_enpit/myscheduler/MainActivity.java b/MyScheduler/app/src/main/java/com/example/yu_enpit/myscheduler/MainActivity.java index e2cc55a..1af7c9f 100644 --- a/MyScheduler/app/src/main/java/com/example/yu_enpit/myscheduler/MainActivity.java +++ b/MyScheduler/app/src/main/java/com/example/yu_enpit/myscheduler/MainActivity.java @@ -1,15 +1,20 @@ package com.example.yu_enpit.myscheduler; +import android.content.Intent; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; -import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; -import android.view.Menu; -import android.view.MenuItem; +import android.widget.AdapterView; +import android.widget.ListView; + +import io.realm.Realm; +import io.realm.RealmResults; public class MainActivity extends AppCompatActivity { + private Realm mRealm; + private ListView mListView; @Override protected void onCreate(Bundle savedInstanceState) { @@ -22,31 +27,28 @@ protected void onCreate(Bundle savedInstanceState) { fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) - .setAction("Action", null).show(); + startActivity(new Intent(MainActivity.this,ScheduleEditActivity.class)); + } + }); + mRealm = Realm.getDefaultInstance(); + + mListView = (ListView) findViewById(R.id.listView); + RealmResults schedules = mRealm.where(Schedule.class).findAll(); + ScheduleAdapter adapter = new ScheduleAdapter(schedules); + mListView.setAdapter(adapter); + + mListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){ + @Override + public void onItemClick(AdapterView parent, View view, int position, long id){ + Schedule schedule = (Schedule) parent.getItemAtPosition(position); + startActivity(new Intent(MainActivity.this, ScheduleEditActivity.class).putExtra("schedule_id", schedule.getId())); } }); } @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_main, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - // Handle action bar item clicks here. The action bar will - // automatically handle clicks on the Home/Up button, so long - // as you specify a parent activity in AndroidManifest.xml. - int id = item.getItemId(); - - //noinspection SimplifiableIfStatement - if (id == R.id.action_settings) { - return true; - } - - return super.onOptionsItemSelected(item); + public void onDestroy() { + super.onDestroy(); + mRealm.close(); } } diff --git a/MyScheduler/app/src/main/java/com/example/yu_enpit/myscheduler/MySchedulerApplication.java b/MyScheduler/app/src/main/java/com/example/yu_enpit/myscheduler/MySchedulerApplication.java new file mode 100644 index 0000000..60a9831 --- /dev/null +++ b/MyScheduler/app/src/main/java/com/example/yu_enpit/myscheduler/MySchedulerApplication.java @@ -0,0 +1,20 @@ +package com.example.yu_enpit.myscheduler; + +import android.app.Application; + +import io.realm.Realm; +import io.realm.RealmConfiguration; + +/** + * Created by v071ff on 2017/08/04. + */ + +public class MySchedulerApplication extends Application { + @Override + public void onCreate(){ + super.onCreate(); + Realm.init(this); + RealmConfiguration realmConfig = new RealmConfiguration.Builder().build(); + Realm.setDefaultConfiguration(realmConfig); + } +} diff --git a/MyScheduler/app/src/main/java/com/example/yu_enpit/myscheduler/Schedule.java b/MyScheduler/app/src/main/java/com/example/yu_enpit/myscheduler/Schedule.java new file mode 100644 index 0000000..faea12e --- /dev/null +++ b/MyScheduler/app/src/main/java/com/example/yu_enpit/myscheduler/Schedule.java @@ -0,0 +1,50 @@ +package com.example.yu_enpit.myscheduler; + +import java.util.Date; + +import io.realm.RealmObject; +import io.realm.annotations.PrimaryKey; + +/** + * Created by v071ff on 2017/08/04. + */ + +public class Schedule extends RealmObject { + @PrimaryKey + private long id; + private Date date; + private String title; + private String detail; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDetail() { + return detail; + } + + public void setDetail(String detail) { + this.detail = detail; + } +} diff --git a/MyScheduler/app/src/main/java/com/example/yu_enpit/myscheduler/ScheduleAdapter.java b/MyScheduler/app/src/main/java/com/example/yu_enpit/myscheduler/ScheduleAdapter.java new file mode 100644 index 0000000..21a3969 --- /dev/null +++ b/MyScheduler/app/src/main/java/com/example/yu_enpit/myscheduler/ScheduleAdapter.java @@ -0,0 +1,49 @@ +package com.example.yu_enpit.myscheduler; + +import android.support.annotation.Nullable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import java.text.SimpleDateFormat; + +import io.realm.OrderedRealmCollection; +import io.realm.RealmBaseAdapter; + +/** + * Created by v071ff on 2017/08/04. + */ + +public class ScheduleAdapter extends RealmBaseAdapter { + + private static class ViewHolder{ + TextView date; + TextView title; + } + + public ScheduleAdapter(@Nullable OrderedRealmCollection data) { + super(data); + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + ViewHolder viewHolder; + if(convertView == null){ + convertView = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_2, parent, false); + viewHolder = new ViewHolder(); + viewHolder.date = (TextView) convertView.findViewById(android.R.id.text1); + viewHolder.title = (TextView) convertView.findViewById(android.R.id.text2); + convertView.setTag(viewHolder); + } else { + viewHolder = (ViewHolder)convertView.getTag(); + } + + Schedule schedule = adapterData.get(position); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); + String formatDate = sdf.format(schedule.getDate()); + viewHolder.date.setText(formatDate); + viewHolder.title.setText(schedule.getTitle()); + return convertView; + } +} diff --git a/MyScheduler/app/src/main/java/com/example/yu_enpit/myscheduler/ScheduleEditActivity.java b/MyScheduler/app/src/main/java/com/example/yu_enpit/myscheduler/ScheduleEditActivity.java new file mode 100644 index 0000000..0d0522c --- /dev/null +++ b/MyScheduler/app/src/main/java/com/example/yu_enpit/myscheduler/ScheduleEditActivity.java @@ -0,0 +1,112 @@ +package com.example.yu_enpit.myscheduler; + +import android.graphics.Color; +import android.os.Bundle; +import android.support.design.widget.Snackbar; +import android.support.v7.app.AppCompatActivity; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.Toast; + +import java.text.SimpleDateFormat; +import java.text.ParseException; +import java.util.Date; + +import io.realm.Realm; +import io.realm.RealmResults; + +public class ScheduleEditActivity extends AppCompatActivity { + private Realm mRealm; + EditText mDateEdit; + EditText mTitleEdit; + EditText mDetailEdit; + Button mDelete; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_schedule_edit); + mRealm = Realm.getDefaultInstance(); + mDateEdit = (EditText)findViewById(R.id.dateEdit); + mTitleEdit = (EditText)findViewById(R.id.titleEdit); + mDetailEdit = (EditText)findViewById(R.id.detailEdit); + mDelete = (Button) findViewById(R.id.delete); + + long scheduleId = getIntent().getLongExtra("schedule_id", -1); + if(scheduleId != -1){ + RealmResults results = mRealm.where(Schedule.class).equalTo("id", scheduleId).findAll(); + Schedule schedule = results.first(); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); + String date = sdf.format(schedule.getDate()); + mDateEdit.setText(date); + mTitleEdit.setText(schedule.getTitle()); + mDetailEdit.setText(schedule.getDetail()); + mDelete.setVisibility(View.VISIBLE); + } else{ + mDelete.setVisibility(View.INVISIBLE); + } + } + + public void onSaveTapped(View view){ + SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); + Date dateParse = new Date(); + try{ + dateParse = sdf.parse(mDateEdit.getText().toString()); + } catch (ParseException e){ + e.printStackTrace(); + } + final Date date = dateParse; + + long scheduleId = getIntent().getLongExtra("schedule_id", -1); + if(scheduleId != -1){ + final RealmResults results = mRealm.where(Schedule.class).equalTo("id", scheduleId).findAll(); + mRealm.executeTransaction(new Realm.Transaction(){ + @Override + public void execute(Realm realm){ + Schedule schedule = results.first(); + schedule.setDate(date); + schedule.setTitle(mTitleEdit.getText().toString()); + schedule.setDetail(mDetailEdit.getText().toString()); + } + }); + Snackbar.make(findViewById(android.R.id.content),"アップデートしました", Snackbar.LENGTH_LONG).setAction("戻る", new View.OnClickListener(){ + @Override + public void onClick(View v){ + finish(); + } + }).setActionTextColor(Color.YELLOW).show(); + } else { + + mRealm.executeTransaction(new Realm.Transaction() { + @Override + public void execute(Realm realm) { + Number maxId = realm.where(Schedule.class).max("id"); + long nextId = 0; + if (maxId != null) nextId = maxId.longValue() + 1; + Schedule schedule = realm.createObject(Schedule.class, new Long(nextId)); + schedule.setDate(date); + schedule.setTitle(mTitleEdit.getText().toString()); + schedule.setDetail(mDetailEdit.getText().toString()); + } + }); + Toast.makeText(this, "追加しました", Toast.LENGTH_SHORT).show(); + finish(); + } + } + + public void onDeleteTapped(View view){ + final long scheduleId = getIntent().getLongExtra("schedule_id", -1); + if(scheduleId != -1){ + mRealm.executeTransaction(new Realm.Transaction(){ + @Override + public void execute(Realm realm){ + Schedule schedule = realm.where(Schedule.class).equalTo("id", scheduleId).findFirst(); + schedule.deleteFromRealm(); + } + }); + Toast.makeText(this, "削除しました", Toast.LENGTH_LONG).show(); + finish(); + } + } +} diff --git a/MyScheduler/app/src/main/res/layout/activity_main.xml b/MyScheduler/app/src/main/res/layout/activity_main.xml index 3be7643..a2019f7 100644 --- a/MyScheduler/app/src/main/res/layout/activity_main.xml +++ b/MyScheduler/app/src/main/res/layout/activity_main.xml @@ -28,6 +28,6 @@ android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" - app:srcCompat="@android:drawable/ic_dialog_email" /> + app:srcCompat="@android:drawable/ic_input_add" /> diff --git a/MyScheduler/app/src/main/res/layout/activity_schedule_edit.xml b/MyScheduler/app/src/main/res/layout/activity_schedule_edit.xml new file mode 100644 index 0000000..f804fed --- /dev/null +++ b/MyScheduler/app/src/main/res/layout/activity_schedule_edit.xml @@ -0,0 +1,111 @@ + + + + + + + + + + + + + + + +