1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Android Studio ToDoListアプリ制作(動画有り)

Last updated at Posted at 2021-03-18

今回はToDoListアプリを制作しました。
YouTubeに作っている動画をアップしています。
https://www.youtube.com/watch?v=HyNPhjL53QY&list=PLhg2PHSq8bjiV6wEal7OVfNEfi520OAza

strings.xml
<resources>
    <string name="app_name">ToDoListApp</string>
    <string name="lbl_title">タイトル:</string>
    <string name="lbl_content">内容:</string>
    <string name="btn_input_register">登録</string>
    <string name="toast_register">登録しました</string>
    <string name="toast_failed">登録出来ませんでした</string>

    <string name="hint_title">タイトルを入力してください</string>
    <string name="hint_content">内容を入力してください</string>
    <string name="btn_input_to_list">一覧画面へ</string>
    <string name="lbl_empty">表示するTODOはありません</string>

    <string name="btn_update">更新</string>
    <string name="toast_update">更新しました</string>

    <string name="btn_delete">削除</string>
    <string name="toast_delete">削除しました</string>
</resources>
list_row.xml
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:id="@+id/list_row"></TextView>

</LinearLayout>
ToDoOpenHelper.java
package com.example.todolistapp.helper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class ToDoOpenHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "DATABASE";
    public static final int VERSION = 1;
    public static final String CREATE_TABLE = "CREATE TABLE 'ToDoList' ('_id' INTEGER PRIMARY KEY AUTOINCREMENT , 'title' TEXT , 'content' TEXT);";

    public ToDoOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lbl_title"></TextView>

        <EditText
            android:id="@+id/edit_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_title"></EditText>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lbl_content"></TextView>

        <EditText
            android:id="@+id/edit_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_content"></EditText>

    </LinearLayout>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="register"
        android:text="@string/btn_input_register"></Button>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="toList"
        android:text="@string/btn_input_to_list"></Button>

</LinearLayout>
MainActivity.java
package com.example.todolistapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentProviderClient;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.todolistapp.helper.ToDoOpenHelper;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void toList(View view) {
        Intent intent = new Intent(MainActivity.this, ListActivity.class);
        startActivity(intent);
    }

    public void register(View view) {
        EditText editTitle = findViewById(R.id.edit_title);
        EditText editContent = findViewById(R.id.edit_content);

        SQLiteDatabase database = null;

        try {
            ToDoOpenHelper helper = new ToDoOpenHelper(MainActivity.this);
            database = helper.getWritableDatabase();

            ContentValues contentValues = new ContentValues();
            contentValues.put("title", editTitle.getText().toString());
            contentValues.put("content", editContent.getText().toString());

            database.insert("ToDoList", null, contentValues);
        } catch (Exception e) {
            Toast.makeText(MainActivity.this, R.string.toast_failed, Toast.LENGTH_LONG).show();
        } finally {
            database.close();
        }
        Toast.makeText(MainActivity.this, R.string.toast_register, Toast.LENGTH_LONG).show();
    }
}
activity_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ListActivity"
    android:orientation="vertical">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@android:id/list"></ListView>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@android:id/empty"
        android:text="@string/lbl_empty"></TextView>

</LinearLayout>
ListActivity.java
package com.example.todolistapp;


import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.example.todolistapp.helper.ToDoOpenHelper;

public class ListActivity extends android.app.ListActivity {
    Cursor cursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
    }

    @Override
    protected void onResume() {
        super.onResume();
        SQLiteDatabase database = null;
        try{
            SQLiteOpenHelper helper = new ToDoOpenHelper(ListActivity.this);
            database = helper.getReadableDatabase();

            cursor = database.query("ToDoList",null,null,null,null,null,null);
            SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                                                   ListActivity.this,
                                                   R.layout.list_row,
                                                   cursor,
                                                   new String[]{"title"},
                                                   new int[]{R.id.list_row},
                                                   CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
            setListAdapter(adapter);
        }catch (Exception e){
            Log.e("エラー",e.getMessage(),e);
        }finally {
            database.close();
        }
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Intent intent = new Intent(ListActivity.this,ContentActivity.class);
        intent.putExtra("id",id);
        startActivity(intent);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(cursor != null || !cursor.isClosed()){
            cursor.close();
        }
    }
}
activity_content.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ContentActivity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lbl_title"></TextView>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/text_title"></EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lbl_content"></TextView>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/text_content"></EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_gravity="right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_marginRight="12dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="update"
            android:text="@string/btn_update"></Button>

        <Button
            android:layout_marginRight="12dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="delete"
            android:text="@string/btn_delete"></Button>

    </LinearLayout>

</LinearLayout>
ContentActivity.java
package com.example.todolistapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.todolistapp.helper.ToDoOpenHelper;

public class ContentActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Long id = getIntent().getLongExtra("id", 0L);

        SQLiteOpenHelper helper = null;
        SQLiteDatabase database = null;
        Cursor cursor = null;

        try {
            helper = new ToDoOpenHelper(ContentActivity.this);
            database = helper.getReadableDatabase();

            cursor = database.query(
                           "ToDoList", 
                           null, 
                           "_id=?", 
                           new String[]{String.valueOf(id)}, 
                           null, 
                           null, 
                           null);
            if (cursor.moveToFirst()) {
                int columnIndex = cursor.getColumnIndex("title");
                String strTitle = cursor.getString(columnIndex);
                columnIndex = cursor.getColumnIndex("content");
                String strContent = cursor.getString(columnIndex);

                EditText titleView = findViewById(R.id.text_title);
                EditText contentView = findViewById(R.id.text_content);

                titleView.setText(strTitle);
                contentView.setText(strContent);
            }
        } catch (Exception e) {
            Log.e("エラー", e.getMessage(), e);
        } finally {
            cursor.close();
            database.close();
        }
    }

    public void update(View view) {
        SQLiteOpenHelper helperUpdate = null;
        SQLiteDatabase databaseUpdate = null;

        String title = ((EditText) findViewById(R.id.text_title)).getText().toString();
        String content = ((EditText) findViewById(R.id.text_content)).getText().toString();

        try {
            helperUpdate = new ToDoOpenHelper(ContentActivity.this);
            databaseUpdate = helperUpdate.getWritableDatabase();

            ContentValues contentValues = new ContentValues();
            contentValues.put("title", title);
            contentValues.put("content", content);

            int updateCount = databaseUpdate.update(
                           "ToDoList", 
                           contentValues, 
                           "_id=?", 
                           new String[]{String.valueOf(getIntent().getLongExtra("id", 0L))});

            if (updateCount == 1) {
                Toast.makeText(ContentActivity.this, R.string.toast_update, Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(ContentActivity.this, R.string.toast_failed, Toast.LENGTH_LONG).show();
            }
            finish();
        } catch (Exception e) {
            Log.e("エラー", e.getMessage(), e);
        } finally {
            databaseUpdate.close();
        }
    }

    public void delete(View view) {

        SQLiteOpenHelper helperDelete = null;
        SQLiteDatabase databaseDelete = null;

        try {
            helperDelete = new ToDoOpenHelper(ContentActivity.this);
            databaseDelete = helperDelete.getWritableDatabase();

            int deleteCount = databaseDelete.delete(
                           "ToDoList", 
                           "_id=?", 
                           new String[]{String.valueOf(getIntent().getLongExtra("id", 0L))});

            if (deleteCount == 1) {
                Toast.makeText(ContentActivity.this, R.string.toast_delete, Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(ContentActivity.this, R.string.toast_failed, Toast.LENGTH_LONG).show();
            }
            finish();
        } catch (Exception e) {
            Log.e("エラー", e.getMessage(), e);
        } finally {
            databaseDelete.close();
        }

    }
}

YouTubeに作っている動画をアップしています。
https://www.youtube.com/watch?v=HyNPhjL53QY&list=PLhg2PHSq8bjiV6wEal7OVfNEfi520OAza
以上。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?