度々お世話になっております。
#質問の概要
こちらのサイトに載せられているプログラムを変更し、SQLiteのDBに保存するものを2つ(英語の格納、日本語の格納)に変更し、サンプルのように表示されるのは英語(例えばcat)で、それをタップするとトースト表示で日本語(例えば猫)が表示されるようにし、英単語暗記のアプリを作成したいと思っています。
そこで、自分なりにプログラムを書き換えたのですが、コンパイルして実機(ZenFone)で実行すると「アプリケーションが終了しました」とエラーが出てしまいます。
追記:デバッグしたところ、MainAvtivity.javaのloadNote()内において、c.getString(c.getColumnIndex(DBAdapter.LASTUPDATE = "lastupdate)),としている行でアプリが強制終了されていることがわかりました。エラーは以下のものが出ています。
#コード
package com.example.username.memoword; //文字数の関係上、importの部分は消して投稿させていただきます。参考にしたサイトとまったく同じように書いております。 public class MainActivity extends Activity implements OnClickListener { static final String TAG = "SQLiteTest1"; static final int MENUITEM_ID_DELETE = 1; ListView itemListView; EditText EnglishEdit; EditText JapaneseEdit; Button saveButton; static DBAdapter dbAdapter; static NoteListAdapter listAdapter; static List<Note> noteList = new ArrayList<Note>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViews(); setListeners(); dbAdapter = new DBAdapter(this); listAdapter = new NoteListAdapter(); itemListView.setAdapter(listAdapter); loadNote(); } protected void findViews(){ itemListView = (ListView)findViewById(R.id.itemListView); EnglishEdit = (EditText)findViewById(R.id.EnglishEdit); JapaneseEdit = (EditText)findViewById(R.id.JapaneseEdit); saveButton = (Button)findViewById(R.id.saveButton); } protected void loadNote(){ noteList.clear(); // Read dbAdapter.open(); Cursor c = dbAdapter.getAllNotes(); startManagingCursor(c); if(c.moveToFirst()){ do { Note note = new Note( c.getInt(c.getColumnIndex(DBAdapter.COL_ID)), c.getString(c.getColumnIndex(DBAdapter.COL_NOTE)), c.getString(c.getColumnIndex(DBAdapter.COL_NOTE2)), c.getString(c.getColumnIndex(DBAdapter.COL_LASTUPDATE)) ); noteList.add(note); } while(c.moveToNext()); } stopManagingCursor(c); dbAdapter.close(); listAdapter.notifyDataSetChanged(); } protected void saveItem(){ dbAdapter.open(); dbAdapter.saveNote(EnglishEdit.getText().toString(),JapaneseEdit.getText().toString()); dbAdapter.close(); EnglishEdit.setText(""); JapaneseEdit.setText(""); loadNote(); } protected void setListeners(){ saveButton.setOnClickListener(this); itemListView.setOnCreateContextMenuListener( new OnCreateContextMenuListener(){ @Override public void onCreateContextMenu( ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.add(0, MENUITEM_ID_DELETE, 0, "削除"); } }); } @Override public boolean onContextItemSelected(MenuItem item) { switch(item.getItemId()){ case MENUITEM_ID_DELETE: AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); Note note = noteList.get(menuInfo.position); final int noteId = note.getId(); new AlertDialog.Builder(this) .setTitle("この単語を削除しますか?") .setPositiveButton( "はい", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dbAdapter.open(); if(dbAdapter.deleteNote(noteId)){ Toast.makeText( getBaseContext(), "単語は正常に削除されました.", Toast.LENGTH_SHORT); loadNote(); } dbAdapter.close(); } }) .setNegativeButton( "キャンセル", null) .show(); return true; } return super.onContextItemSelected(item); } @Override public void onClick(View v) { switch(v.getId()){ case R.id.saveButton: saveItem(); break; } } private class NoteListAdapter extends BaseAdapter { @Override public int getCount() { return noteList.size(); } @Override public Object getItem(int position) { return noteList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { TextView noteTextView; TextView lastupdateTextView; View v = convertView; if(v==null){ LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = inflater.inflate(R.layout.row, null); } Note note = (Note)getItem(position); if(note != null){ noteTextView = (TextView)v.findViewById(R.id.noteTextView); lastupdateTextView = (TextView)v.findViewById( R.id.lastupdateTextView); noteTextView.setText(note.getNote()); noteTextView.setText(note.getNote2()); lastupdateTextView.setText(note.getLastupdate()); } return v; } } }
package com.example.username.memoword; import java.util.Date; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBAdapter { static final String DATABASE_NAME = "mynote.db"; static final int DATABASE_VERSION = 1; public static final String TABLE_NAME = "notes"; public static final String COL_ID = "_id"; public static final String COL_NOTE = "note"; public static final String COL_NOTE2 = "note2"; public static final String COL_LASTUPDATE = "lastupdate"; protected final Context context; protected DatabaseHelper dbHelper; protected SQLiteDatabase db; public DBAdapter(Context context){ this.context = context; dbHelper = new DatabaseHelper(this.context); } private static class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL( "CREATE TABLE " + TABLE_NAME + " (" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COL_NOTE + " TEXT NOT NULL," + COL_NOTE2 + " TEXT NOT NULL," + COL_LASTUPDATE + " TEXT NOT NULL);"); } @Override public void onUpgrade( SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } } public DBAdapter open() { db = dbHelper.getWritableDatabase(); return this; } public void close(){ dbHelper.close(); } public boolean deleteAllNotes(){ return db.delete(TABLE_NAME, null, null) > 0; } public boolean deleteNote(int id){ return db.delete(TABLE_NAME, COL_ID + "=" + id, null) > 0; } public Cursor getAllNotes(){ return db.query(TABLE_NAME, null, null, null, null, null, null); } public void saveNote(String note,String note2){ Date dateNow = new Date (); ContentValues values = new ContentValues(); values.put(COL_NOTE, note); values.put(COL_NOTE2, note2); values.put(COL_LASTUPDATE, dateNow.toLocaleString()); db.insertOrThrow(TABLE_NAME, null, values); } }
package com.example.username.memoword; public class Note { protected int id; protected String note; protected String note2; protected String lastupdate; public Note(int id, String note, String note2, String lastupdate){ this.id = id; this.note = note; this.note2 = note2; this.lastupdate = lastupdate; } public String getNote(){ return note; } public String getNote2(){ return note2; } public String getLastupdate(){ return lastupdate; } public int getId(){ return id; } }
回答1件
あなたの回答
tips
プレビュー