package com.example.e03list;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends ActionBarActivity {

	LectureDBOpenHelper helper;
	SQLiteDatabase db;
	Cursor cursor;
	SimpleCursorAdapter adapter;

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

		adapter = new SimpleCursorAdapter(this, R.layout.list_item, null,
				new String[] { "title", "modifiedTime", "_id" }, new int[] {
						R.id.text_view1, R.id.text_view2, R.id.text_view3 },
				CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
		ListView listView = (ListView) findViewById(R.id.listView1);
		listView.setAdapter(adapter);

		Button b = (Button) findViewById(R.id.button1);
		View.OnClickListener onClick = new OnClickListener() {
			public SQLiteDatabase db1 = db;
			
			@Override
			public void onClick(View v) {
				Log.e("db1", "db1 : " + db1 + " db : " + db); 
				// db1은 대입하여 메소드 호출하는 식
				// db는 내부 익명 클래스에서 외부 멤버변수를 호출하는 형식이라 
				// resume때 생성해도 상관이 없다.

				EditText e = (EditText) findViewById(R.id.editText1);
				String str = e.getText().toString();
				e.setText("");
				ContentValues value = new ContentValues(1);
				value.put("title", str);
				db.insert("ListItem", null, value);
				cursor.close();
				cursor = db.rawQuery("select * from ListItem", null);
				adapter.changeCursor(cursor);
			}
		};
		b.setOnClickListener(onClick);
	}

	@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;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onResume() {
		super.onResume();
		helper = new LectureDBOpenHelper(this);
		db = helper.getWritableDatabase();
		cursor = db.rawQuery("SELECT * FROM ListItem", null);
		adapter.changeCursor(cursor);
	}

    @Override
    protected void onPause() {
        super.onPause();
        cursor.close(); 
        db.close();
        helper.close();
    }
}

'J > Java' 카테고리의 다른 글

자바 유용한 라이브러리  (0) 2014.12.11
Java 내부 분석  (0) 2014.10.12
jackson generic type 관련  (0) 2014.08.24
URLConnection을 이용한 이미지 추출 프로그램  (0) 2014.06.28
Static 키워드  (0) 2014.03.18

설정

트랙백

댓글