public class MainActivity extends Activity {
private SQLiteDatabase db = null;
private ListView listView = null;
private EditText editText = null;
private MyCursorAdapter adapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText1);
Button btn = (Button) findViewById(R.id.button1);
// クリック処理(匿名クラス)
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
saveText();
displayData(DatabaseHelper.C_ID + " DESC");
editText.setFocusable(false);
}
});
//ListView を取得
listView = (ListView) findViewById(R.id.listView1);
// リストビューのアイテムがクリックされた時に呼び出されるコールバックリスナーを登録します
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ListView listView = (ListView) parent;
Cursor cursor = (Cursor)listView.getItemAtPosition(position);
String item = cursor.getString(cursor.getColumnIndex(DatabaseHelper.C_ID));
Toast.makeText(MainActivity.this, item, Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onStart() {
// TODO 自動生成されたメソッド・スタブ
super.onStart();
displayData(DatabaseHelper.C_ID + " DESC");
}
public void displayData(String sort){
DatabaseHelper helper = new DatabaseHelper(this);
db = helper.getReadableDatabase();
//列名の定義
String[] from = {DatabaseHelper.C_ON_OFF,
DatabaseHelper.C_TIME,
DatabaseHelper.C_ID};
Cursor cursor = db.query(DatabaseHelper.TABLE_NAME, null, null, null, null, null, sort);
//String[] from = {"head", "body"}; //DBのheadとbodyというColumnのデータを
int[] to = {R.id.todo_check,
R.id.tv1,
R.id.tv2}; //R.id.~に埋めます
adapter = new MyCursorAdapter(this, R.layout.row, cursor, from, to, 0);
listView.setAdapter(adapter);
db.close();
}
public void saveText(){
DatabaseHelper helper = new DatabaseHelper(this);
db = helper.getWritableDatabase();
String text = editText.getText().toString().trim();
if (!text.equals("")) {
String strDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss",Locale.US).format(new Date());
String sql = "insert into " +
DatabaseHelper.TABLE_NAME +
"(" +
DatabaseHelper.C_TIME + "," +
DatabaseHelper.C_DATE_ENTRY + ")" +
" values(" +
"'" + text + "'," +
"'" + strDate + "');" ;
try {
db.execSQL(sql);
Toast.makeText(this,"登録しました!", Toast.LENGTH_SHORT).show();
// 登録に成功したら次の入力に備えて空にします
editText.setText("");
} catch (SQLException e) {
Toast.makeText(this,"登録に失敗しました・・・", Toast.LENGTH_SHORT).show();
Log.e("ERROR", e.toString());
}
}else{
Toast.makeText(this,"入力されていません。", Toast.LENGTH_SHORT).show();
}
db.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
public class MyCursorAdapter extends SimpleCursorAdapter {
private CheckBox checkBox;
public MyCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
// TODO 自動生成されたコンストラクター・スタブ
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = super.newView(context, cursor, parent);
checkBox = (CheckBox) view.findViewById(R.id.todo_check);
checkBox.setFocusable(false);
checkBox.setFocusableInTouchMode(false);
checkBox.setOnClickListener(new onCheckBoxClickListener());
return view;
}
public class onCheckBoxClickListener implements OnClickListener {
public void onClick(View v) {
Log.e("MyCursorAdapter", String.valueOf(checkBox.isChecked()));
}
}
public class MyViewBinder implements SimpleCursorAdapter.ViewBinder {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (columnIndex == cursor.getColumnIndex(DatabaseHelper.C_ON_OFF)) {
boolean check = (cursor.getString(columnIndex).equals("1"));
checkBox.setChecked(check);
return true;
}
return false;
}
}
}