こちらのサイトリンク内容
に載せられているコードを変更し、英単語帳のようなandroidアプリを作成しようとしています。
ShowDataBase.java
1package com.example.wordmemo; 2 3import android.app.Activity; 4import android.database.Cursor; 5import android.database.sqlite.SQLiteDatabase; 6import android.os.Bundle; 7import android.widget.LinearLayout; 8import android.widget.TextView; 9 10public class ShowDataBase extends Activity { 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.show_database); 15 16 LinearLayout layout = new LinearLayout(this); 17 layout.setOrientation(LinearLayout.VERTICAL); 18 setContentView(layout); 19 20 MyOpenHelper helper = new MyOpenHelper(this); 21 SQLiteDatabase db = helper.getReadableDatabase(); 22 23 // queryメソッドの実行例 24 Cursor c = db.query("words", new String[] { "eng", "jpn" }, null, 25 null, null, null, null); 26 27 boolean mov = c.moveToFirst(); 28 while (mov) { 29 TextView textView = new TextView(this); 30 textView.setText(String.format("%s : %s", c.getString(0), c.getString(1))); 31 mov = c.moveToNext(); 32 layout.addView(textView); 33 } 34 c.close(); 35 db.close(); 36 } 37}
MainActivity.java
1package com.example.wordmemo; 2 3import android.app.Activity; 4import android.content.ContentValues; 5import android.content.Intent; 6import android.database.sqlite.SQLiteDatabase; 7import android.os.Bundle; 8import android.view.View; 9import android.view.View.OnClickListener; 10import android.widget.Button; 11import android.widget.EditText; 12import android.widget.Toast; 13 14public class MainActivity extends Activity { 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 21 MyOpenHelper helper = new MyOpenHelper(this); 22 final SQLiteDatabase db = helper.getWritableDatabase(); 23 24 final EditText engText = (EditText) findViewById(R.id.editEnglish); //nameText=engText 25 final EditText jpnText = (EditText) findViewById(R.id.editJapanese); //ageText=jpnText 26 27 Button entryButton = (Button) findViewById(R.id.insert); 28 entryButton.setOnClickListener(new OnClickListener() { 29 30 @Override 31 public void onClick(View v) { 32 String eng = engText.getText().toString(); 33 String jpn = jpnText.getText().toString(); 34 35 ContentValues insertValues = new ContentValues(); 36 insertValues.put("eng", eng); 37 insertValues.put("jpn", jpn); 38 long id = db.insert("words", eng, insertValues); 39 } 40 }); 41 42 Button updateButton = (Button) findViewById(R.id.update); 43 updateButton.setOnClickListener(new OnClickListener() { 44 45 @Override 46 public void onClick(View v) { 47 String eng = engText.getText().toString(); 48 String jpn = jpnText.getText().toString(); 49 50 if (eng.equals("")) { 51 Toast.makeText(MainActivity.this, "英語を入力してください。", 52 Toast.LENGTH_SHORT).show(); 53 } else { 54 ContentValues updateValues = new ContentValues(); 55 updateValues.put("jpn", jpn); 56 db.update("words", updateValues, "eng=?", new String[] { eng }); 57 } 58 } 59 }); 60 61 Button deleteButton = (Button) findViewById(R.id.delete); 62 deleteButton.setOnClickListener(new OnClickListener() { 63 64 @Override 65 public void onClick(View v) { 66 String eng = engText.getText().toString(); 67 String jpn = jpnText.getText().toString(); 68 69 if (eng.equals("")) { 70 Toast.makeText(MainActivity.this, "英語を入力してください。", 71 Toast.LENGTH_SHORT).show(); 72 } else { 73 db.delete("words", "eng=?", new String[] { eng }); 74 } 75 76 } 77 }); 78 79 Button deleteAllButton = (Button) findViewById(R.id.AllDelete); 80 deleteAllButton.setOnClickListener(new OnClickListener() { 81 82 @Override 83 public void onClick(View v) { 84 String eng = engText.getText().toString(); 85 String jpn = jpnText.getText().toString(); 86 87 db.delete("words", null, null); 88 89 } 90 }); 91 92 Button detaBaseButton = (Button) findViewById(R.id.dataBase); 93 detaBaseButton.setOnClickListener(new OnClickListener() { 94 95 @Override 96 public void onClick(View v) { 97 Intent dbIntent = new Intent(MainActivity.this, 98 ShowDataBase.class); 99 startActivity(dbIntent); 100 101 } 102 }); 103 } 104}
MyOpenHelper.java
1package com.example.wordmemo; 2 3import android.content.Context; 4import android.database.sqlite.SQLiteDatabase; 5import android.database.sqlite.SQLiteOpenHelper; 6 7public class MyOpenHelper extends SQLiteOpenHelper { 8 9 public MyOpenHelper(Context context) { 10 super(context, "SaveDB", null, 1); 11 } 12 13 @Override 14 public void onCreate(SQLiteDatabase db) { 15 db.execSQL("create table words(" + " eng text not null," + "jpn text " 16 + ");"); 17 } 18 19 @Override 20 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 21 22 } 23 24}
activity_main.xml
1<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:app="http://schemas.android.com/apk/res-auto" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 7 tools:context=".MainActivity" > 8 9 <TextView 10 android:id="@+id/textView1" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:layout_alignParentLeft="true" 14 android:layout_alignParentTop="true" 15 android:layout_alignParentRight="true" 16 android:layout_marginLeft="0dp" 17 android:layout_marginTop="40dp" 18 android:layout_marginRight="0dp" 19 android:text="英単語を登録する" 20 android:textAppearance="?android:attr/textAppearanceMedium" /> 21 22~文字数制限のため省略~ 23 24</LinearLayout>
これらをコンパイルし、アプリの中でデータベースを開こうとすると、「アプリケーションが終了しました」と出てきて変えることができません。参考にしたサイトでは、MyOpenHelperのことを”SQLiteOpenHelperを継承したクラスで”と書いてあるのでその当たりの解釈ができていないのかな・・・?と考えています。わかる方がいましたら教えてくださると幸いです。
追記:SQLiteのデータベースは、テーブル名はwords、要素はeng(英語を格納)とjpn(日本語を格納)の2つとしたいと思っています。IDも必要になれば追加したいと思っております。
追記2:activity_main.xmlの上部はこのように書いております。サイトのコードをそのまま貼ったはずではありますが念のため、、
回答1件
あなたの回答
tips
プレビュー