1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| package com.xuie.greendaodemo;
import android.app.ListActivity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.inputmethod.EditorInfo; import android.widget.CursorAdapter; import android.widget.EditText; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.TextView;
import com.xuie.greendaodemo.greendao.DaoMaster; import com.xuie.greendaodemo.greendao.DaoSession; import com.xuie.greendaodemo.greendao.Note; import com.xuie.greendaodemo.greendao.NoteDao;
import java.text.DateFormat; import java.util.Date;
public class MainActivity extends ListActivity { private EditText editText; private SQLiteDatabase db; private NoteDao noteDao;
private SimpleCursorAdapter adapter;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "notes-db", null); db = helper.getWritableDatabase(); DaoMaster daoMaster = new DaoMaster(db); DaoSession daoSession = daoMaster.newSession(); noteDao = daoSession.getNoteDao();
String textColumn = NoteDao.Properties.Text.columnName; String orderBy = textColumn + " COLLATE LOCALIZED ASC"; Cursor cursor = db.query(noteDao.getTablename(), noteDao.getAllColumns(), null, null, null, null, orderBy); String[] from = {textColumn, NoteDao.Properties.Comment.columnName}; int[] to = {android.R.id.text1, android.R.id.text2};
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); setListAdapter(adapter); cursor.close();
editText = (EditText) findViewById(R.id.editTextNote); addUiListeners(); }
protected void addUiListeners() { editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { addNote(); return true; } return false; } });
final View button = findViewById(R.id.buttonAdd); button.setEnabled(false); editText.addTextChangedListener(new TextWatcher() {
@Override public void onTextChanged(CharSequence s, int start, int before, int count) { boolean enable = s.length() != 0; button.setEnabled(enable); }
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override public void afterTextChanged(Editable s) { } }); }
public void onMyButtonClick(View view) { addNote(); }
private void addNote() { String noteText = editText.getText().toString(); editText.setText("");
final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); String comment = "Added on " + df.format(new Date()); Note note = new Note(null, noteText, comment, new Date()); noteDao.insert(note); Log.d("DaoExample", "Inserted new note, ID: " + note.getId());
changeCursor(); }
@Override protected void onListItemClick(ListView l, View v, int position, long id) { noteDao.deleteByKey(id); Log.d("DaoExample", "Deleted note, ID: " + id); changeCursor(); }
private void changeCursor() { String textColumn = NoteDao.Properties.Text.columnName; String orderBy = textColumn + " COLLATE LOCALIZED ASC"; Cursor cursor = db.query(noteDao.getTablename(), noteDao.getAllColumns(), null, null, null, null, orderBy);
adapter.changeCursor(cursor); adapter.notifyDataSetChanged(); }
}
|