---
# System prepended metadata

title: 'AN25 課題No.07(optionは追加で10,000円です)'
tags: [課題]

---

# AN25 課題No.07(optionは追加で10,000円です)
###### tags: `課題`


activity_main.xml
```xml=
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dip"
        android:gravity="left">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="キー"/>
        <EditText
            android:id="@+id/EditKey"
            android:layout_width="400dp"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="本文" />
    <EditText
        android:id="@+id/EditText01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.93"
        android:gravity="top"/>

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
            <Button
                android:id="@+id/KeyRead"
                android:layout_width="90dp"
                android:layout_height="match_parent"
                android:onClick="functionKeyRead"
                android:text="読む"
                android:textSize="20sp" />
            <Button
                android:id="@+id/KeWrite"
                android:layout_width="90dp"
                android:layout_height="match_parent"
                android:onClick="functionKeyWrite"
                android:text="保存"
                android:textSize="20sp" />
            <Button
                android:id="@+id/KeyClear"
                android:layout_width="90dp"
                android:layout_height="match_parent"
                android:onClick="functionKeyClear"
                android:text="クリア"
                android:textSize="20sp" />
            <Button
                android:id="@+id/KeyList"
                android:layout_width="90dp"
                android:layout_height="match_parent"
                android:onClick="functionKeyList"
                android:text="一覧"
                android:textSize="20sp" />
        </TableRow>
    </TableLayout>

</LinearLayout>
```

Activity_main.xml(option)
```xml=
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dip"
        android:gravity="left">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="キー"/>
        <EditText
            android:id="@+id/EditKey"
            android:layout_width="400dp"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="本文" />
    <EditText
        android:id="@+id/EditText01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.93"
        android:gravity="top"/>

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
            <Button
                android:id="@+id/KeyRead"
                android:layout_width="70dp"
                android:layout_height="match_parent"
                android:onClick="functionKeyRead"
                android:text="読む"
                android:textSize="20sp" />
            <Button
                android:id="@+id/KeWrite"
                android:layout_width="70dp"
                android:layout_height="match_parent"
                android:onClick="functionKeyWrite"
                android:text="保存"
                android:textSize="20sp" />
            <Button
                android:id="@+id/KeyClear"
                android:layout_width="70dp"
                android:layout_height="match_parent"
                android:onClick="functionKeyClear"
                android:text="クリア"
                android:textSize="20sp" />
            <Button
                android:id="@+id/KeyList"
                android:layout_width="70dp"
                android:layout_height="match_parent"
                android:onClick="functionKeyList"
                android:text="一覧"
                android:textSize="20sp" />
            <Button
                android:id="@+id/KeyDelete"
                android:layout_width="70dp"
                android:layout_height="match_parent"
                android:onClick="functionKeyDelete"
                android:text="削除"
                android:textSize="20sp" />
            <Button
                android:id="@+id/KeyUpdate"
                android:layout_width="70dp"
                android:layout_height="match_parent"
                android:onClick="functionKeyUpdate"
                android:text="更新"
                android:textSize="20sp" />

        </TableRow>
    </TableLayout>

</LinearLayout>
```

MainActivity.java
```java=
package nhs00650.hal.ac.mpad;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.HashMap;

import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import android.database.*;
import android.database.sqlite.*;


public class MainActivity extends AppCompatActivity {

    SQLiteDatabase db;
    EditText et;
    EditText ekey;
    int cert = 0;
    String ekeyStr = "";
    String key = "";
    String memo = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et = (EditText) findViewById(R.id.EditText01);
        ekey = (EditText) findViewById(R.id.EditKey);


        //データベースの作成
        String dbStr = "data/data/" + getPackageName() + "/nhs00650.db";
        db = SQLiteDatabase.openOrCreateDatabase(dbStr, null);

        //テーブル作成用のクエリ文
        String query_table1 = "DROP TABLE IF EXISTS mpad";
        String query_table2 = "CREATE TABLE mpad (id INTEGER PRIMARY KEY AUTOINCREMENT," + "keyy TEXT, memo TEXT, write_date TEXT)";

        //テーブルの作成
        db.execSQL(query_table1);
        db.execSQL(query_table2);

        //レコードの追加
        //今日の日付の取得
        String write_dateStr = now_data();
        String query_record_set[] = {
                "INSERT INTO mpad(keyy,memo,write_date) VALUES('test01','テスト01','" + write_dateStr + "')",
                "INSERT INTO mpad(keyy,memo,write_date) VALUES('test02','テスト02','" + write_dateStr + "')",
                "INSERT INTO mpad(keyy,memo,write_date) VALUES('test03','テスト03','" + write_dateStr + "')"
        };

        for (int i = 0; i < query_record_set.length; i++) {
            db.execSQL(query_record_set[i]);
        }
    }

    private String now_data() {
        Date today = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(today);
        int yy = cal.get(Calendar.YEAR);
        int mm = cal.get(Calendar.MONTH) + 1;
        int dd = cal.get(Calendar.DATE);
        int hour = cal.get(Calendar.HOUR);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);

        String now_dateStr = yy + "/" + mm + "/" + dd + " " + hour + ":" + minute + ":" + second;

        return now_dateStr;
    }

    public void functionKeyWrite(View KeyWrite) {
        String write_dateStr = now_data();
        String ekeyStr = ekey.getText().toString();
        String etStr = et.getText().toString();
        String query_insert = "INSERT INTO mpad(keyy,memo,write_date) VALUES('"+ekeyStr + "' , '" + etStr + "' , '" + write_dateStr + "')";
        db.execSQL(query_insert);
        Toast myToast = Toast.makeText(this,
                        etStr + "をキーに書き込みました", Toast.LENGTH_SHORT);
        myToast.show();
    }

    //読むボタンを押した時のメソッド
    public void functionKeyRead(View KeyRead) {
        ekeyStr = ekey.getText().toString();
        //DB検索
        String query_select = "SELECT id, keyy, memo, write_date FROM mpad";
        Cursor db_row = db.rawQuery(query_select, null);

        //レコードを取り出しながらフィールドデータを取得
        String result_memo = "a";
        while (db_row.moveToNext()) {
            int index_id = db_row.getColumnIndex("id");
            int index_key = db_row.getColumnIndex("keyy");
            int index_memo = db_row.getColumnIndex("memo");
            key = db_row.getString(index_key);
            memo = db_row.getString(index_memo);
            //入力されたキーとDBから取得したキーを比較
            if (key.equals(ekeyStr)) {
                result_memo = memo;
            }
        }

        if (result_memo.equals("a")) {
            cert = 0;
            et.setText("");
            toastMethod();
        } else {
            cert = 1;
            et.setText(result_memo);
            toastMethod();
        }
    }


    public void toastMethod() {
        if (cert == 1) {
            Toast myToast = Toast.makeText(this,
                    ekeyStr + "の内容を読み込みました", Toast.LENGTH_SHORT);
            myToast.show();
        } else if (cert == 0) {
            Toast myToast = Toast.makeText(this,
                    "指定されたキーはありません", Toast.LENGTH_SHORT);
            myToast.show();
        }
    }

    public void functionKeyClear(View KeyClear) {
        et.setText("");
    }

    public void functionKeyList(View KeyList) {
        //DB検索
        String query_select = "SELECT id, keyy, memo, write_date FROM mpad";
        Cursor db_row = db.rawQuery(query_select, null);

        //レコードを取り出しながらフィールドデータを取得
        ArrayList<String> list = new ArrayList<>();
        ArrayList<String> list2 = new ArrayList<>();
        ArrayList<String> list3 = new ArrayList<>();

        while (db_row.moveToNext()) {
            int index_id = db_row.getColumnIndex("id");
            int index_key = db_row.getColumnIndex("keyy");
            int index_memo = db_row.getColumnIndex("memo");
            int index_write_date = db_row.getColumnIndex("write_date");
            String id = db_row.getString(index_id);
            key = db_row.getString(index_key);
            memo = db_row.getString(index_memo);
            String write_date = db_row.getString(index_write_date);
            list.add(key + "\n");
            list2.add(memo);
            list3.add(write_date + "\n");
        }
        StringBuilder stb = new StringBuilder();
        for(int i=0; i < list.size(); i++){
            stb.append("key:" + list.get(i) + "本文:" + list2.get(i) + "　　作成日:" + list3.get(i));
        }
        et.setText(stb);
        Toast myToast = Toast.makeText(this,
                "全レコード読み込みました", Toast.LENGTH_SHORT);
        myToast.show();
    }


}
```

MainActivity.java(option)
```java=
package nhs00650.hal.ac.mpad;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.HashMap;

import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import android.database.*;
import android.database.sqlite.*;


public class MainActivity extends AppCompatActivity {

    SQLiteDatabase db;
    EditText et;
    EditText ekey;
    int cert = 0;
    String ekeyStr = "";
    String key = "";
    String memo = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et = (EditText) findViewById(R.id.EditText01);
        ekey = (EditText) findViewById(R.id.EditKey);


        //データベースの作成
        String dbStr = "data/data/" + getPackageName() + "/nhs00650.db";
        db = SQLiteDatabase.openOrCreateDatabase(dbStr, null);

        //テーブル作成用のクエリ文
        String query_table1 = "DROP TABLE IF EXISTS mpad";
        String query_table2 = "CREATE TABLE mpad (id INTEGER PRIMARY KEY AUTOINCREMENT," + "keyy TEXT, memo TEXT, write_date TEXT)";

        //テーブルの作成
        db.execSQL(query_table1);
        db.execSQL(query_table2);

        //レコードの追加
        //今日の日付の取得
        String write_dateStr = now_data();
        String query_record_set[] = {
                "INSERT INTO mpad(keyy,memo,write_date) VALUES('test01','テスト01','" + write_dateStr + "')",
                "INSERT INTO mpad(keyy,memo,write_date) VALUES('test02','テスト02','" + write_dateStr + "')",
                "INSERT INTO mpad(keyy,memo,write_date) VALUES('test03','テスト03','" + write_dateStr + "')"
        };

        for (int i = 0; i < query_record_set.length; i++) {
            db.execSQL(query_record_set[i]);
        }
    }

    private String now_data() {
        Date today = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(today);
        int yy = cal.get(Calendar.YEAR);
        int mm = cal.get(Calendar.MONTH) + 1;
        int dd = cal.get(Calendar.DATE);
        int hour = cal.get(Calendar.HOUR);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);

        String now_dateStr = yy + "/" + mm + "/" + dd + " " + hour + ":" + minute + ":" + second;

        return now_dateStr;
    }

    public void functionKeyWrite(View KeyWrite) {
        String write_dateStr = now_data();
        String ekeyStr = ekey.getText().toString();
        String etStr = et.getText().toString();
        String query_insert = "INSERT INTO mpad(keyy,memo,write_date) VALUES('"+ekeyStr + "' , '" + etStr + "' , '" + write_dateStr + "')";
        db.execSQL(query_insert);
        Toast myToast = Toast.makeText(this,
                        etStr + "をキーに書き込みました", Toast.LENGTH_SHORT);
        myToast.show();
    }

    //読むボタンを押した時のメソッド
    public void functionKeyRead(View KeyRead) {
        ekeyStr = ekey.getText().toString();
        //DB検索
        String query_select = "SELECT id, keyy, memo, write_date FROM mpad";
        Cursor db_row = db.rawQuery(query_select, null);

        //レコードを取り出しながらフィールドデータを取得
        String result_memo = "a";
        while (db_row.moveToNext()) {
            int index_id = db_row.getColumnIndex("id");
            int index_key = db_row.getColumnIndex("keyy");
            int index_memo = db_row.getColumnIndex("memo");
            key = db_row.getString(index_key);
            memo = db_row.getString(index_memo);
            //入力されたキーとDBから取得したキーを比較
            if (key.equals(ekeyStr)) {
                result_memo = memo;
            }
        }

        if (result_memo.equals("a")) {
            cert = 0;
            et.setText("");
            toastMethod();
        } else {
            cert = 1;
            et.setText(result_memo);
            toastMethod();
        }
    }


    public void toastMethod() {
        if (cert == 1) {
            Toast myToast = Toast.makeText(this,
                    ekeyStr + "の内容を読み込みました", Toast.LENGTH_SHORT);
            myToast.show();
        } else if (cert == 0) {
            Toast myToast = Toast.makeText(this,
                    "指定されたキーはありません", Toast.LENGTH_SHORT);
            myToast.show();
        }
    }

    public void functionKeyClear(View KeyClear) {
        et.setText("");
    }

    public void functionKeyList(View KeyList) {
        //DB検索
        String query_select = "SELECT id, keyy, memo, write_date FROM mpad";
        Cursor db_row = db.rawQuery(query_select, null);

        //レコードを取り出しながらフィールドデータを取得
        ArrayList<String> list = new ArrayList<>();
        ArrayList<String> list2 = new ArrayList<>();
        ArrayList<String> list3 = new ArrayList<>();

        while (db_row.moveToNext()) {
            int index_id = db_row.getColumnIndex("id");
            int index_key = db_row.getColumnIndex("keyy");
            int index_memo = db_row.getColumnIndex("memo");
            int index_write_date = db_row.getColumnIndex("write_date");
            String id = db_row.getString(index_id);
            key = db_row.getString(index_key);
            memo = db_row.getString(index_memo);
            String write_date = db_row.getString(index_write_date);
            list.add(key + "\n");
            list2.add(memo);
            list3.add(write_date + "\n");
        }
        StringBuilder stb = new StringBuilder();
        for(int i=0; i < list.size(); i++){
            stb.append("key:" + list.get(i) + "本文:" + list2.get(i) + "　　作成日:" + list3.get(i));
        }
        et.setText(stb);
        Toast myToast = Toast.makeText(this,
                "全レコード読み込みました", Toast.LENGTH_SHORT);
        myToast.show();
    }

    public void functionKeyDelete(View KeyDelete) {
        int count = 0;
        ekeyStr = ekey.getText().toString();
        String query_count = "SELECT * FROM mpad WHERE keyy = '" + ekeyStr + "'";
        Cursor db_row = db.rawQuery(query_count, null);
        while (db_row.moveToNext()) {
            int index_key = db_row.getColumnIndex("keyy");
            key = db_row.getString(index_key);
            if(key.equals(ekeyStr)){
                count = 1;
                break;
            } else {
                count = 0;
            }
        }
        if(count == 1){
            String query_record_delete = "DELETE FROM mpad WHERE keyy = '" + ekeyStr +  "'";
            db.execSQL(query_record_delete);
            Toast myToast = Toast.makeText(this,
                    ekeyStr + "を削除しました", Toast.LENGTH_SHORT);
            myToast.show();
        } else if(count == 0){
            Toast myToast = Toast.makeText(this,
                    "指定されたキーはありません", Toast.LENGTH_SHORT);
            myToast.show();
        }
    }

    public void functionKeyUpdate(View KeyUpdate) {
       int count = 0;
        ekeyStr = ekey.getText().toString();
        String etStr = et.getText().toString();
        String query_count = "SELECT * FROM mpad WHERE keyy = '" + ekeyStr + "'";
        Cursor db_row = db.rawQuery(query_count, null);
        while (db_row.moveToNext()) {
            int index_key = db_row.getColumnIndex("keyy");
            key = db_row.getString(index_key);
            if(key.equals(ekeyStr)){
                count = 1;
                break;
            } else {
                count = 0;
            }
        }
        if(count == 1){
            String query_record_update = "UPDATE mpad SET memo = '" + etStr + "' WHERE keyy = '" + ekeyStr +  "'";
            db.execSQL(query_record_update);
            Toast myToast = Toast.makeText(this,
                    ekeyStr + "を更新しました", Toast.LENGTH_SHORT);
            myToast.show();
        } else if(count == 0){
            Toast myToast = Toast.makeText(this,
                    "指定されたキーはありません", Toast.LENGTH_SHORT);
            myToast.show();
        }
    }
}
```


## 雑談 (0→やまぴ 1→みや 2→やすい 3→りょうくん 4→アミカ 5→やの 6→山田)












---
<span style="color: #ff3333">aaa</span>
<span style="text-decoration: underline">aaa</span>