classSQLiteExample

Last-modified: 2015-03-18 (水) 12:38:06
public class SQLiteExample extends Activity implements OnClickListener{

	Button sqlUpdate, sqlView, sqlModify, sqlGetInfo, sqlDelete;
	EditText sqlName, sqlHotness, sqlRow;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自動生成されたメソッド・スタブ
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sqliteexample);
		sqlUpdate = (Button) findViewById (R.id.bSQLUpdate);
		sqlView = (Button) findViewById (R.id.bSQLopenView);
		sqlName = (EditText) findViewById (R.id.etSQLName);
		sqlHotness = (EditText) findViewById (R.id.etSQLHotness);
		sqlUpdate.setOnClickListener(this);
		sqlView.setOnClickListener(this);

		sqlRow = (EditText) findViewById(R.id.etSQLrowInfo);
		sqlModify = (Button) findViewById(R.id.bSQLmodify);
		sqlGetInfo = (Button) findViewById(R.id.bGetInfo);
		sqlDelete = (Button) findViewById(R.id.bSQLdelete);
		sqlModify.setOnClickListener(this);
		sqlGetInfo.setOnClickListener(this);
		sqlDelete.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO 自動生成されたメソッド・スタブ
		switch(v.getId()){

		// input
		case R.id.bSQLUpdate:
			boolean didItWork = true;

			try{
				String name = sqlName.getText().toString();
				String hotness = sqlHotness.getText().toString();

				HotOrNot entry = new HotOrNot(SQLiteExample.this);
				entry.open();
				entry.createEntry(name, hotness);

				entry.close();
			} catch(Exception e){
				didItWork = false;
				String error = e.toString();
				Dialog d = new Dialog(this);
				d.setTitle("Dang it");
				TextView tv = new TextView(this);
				tv.setText(error);
				d.setContentView(tv);
				d.show();

			} finally {
				if(didItWork){
					Dialog d = new Dialog(this);
					d.setTitle("heck yea!");
					TextView tv = new TextView(this);
					tv.setText("Success");
					d.setContentView(tv);
					d.show();
				}
			}
			break;

		// view
		case R.id.bSQLopenView:
			Intent i = new Intent("com.example.startingpoint.SQLVIEW");
			startActivity(i);
			break;

		// get
		case R.id.bGetInfo:
			try{
				String s = sqlRow.getText().toString();
				long l = Long.parseLong(s);
				HotOrNot hon = new HotOrNot(this);
				hon.open();
				String returnedName = hon.getName(l);
				String returnedHotness = hon.getHotness(l);
				hon.close();
				sqlName.setText(returnedName);
				sqlHotness.setText(returnedHotness);
			} catch(Exception e){

				String error = e.toString();
				Dialog d = new Dialog(this);
				d.setTitle("Dang it");
				TextView tv = new TextView(this);
				tv.setText(error);
				d.setContentView(tv);
				d.show();
			}
			break;

		// update
		case R.id.bSQLmodify:
			try{
				String mName = sqlName.getText().toString();
				String mHotness = sqlHotness.getText().toString();

				String sRow = sqlRow.getText().toString();
				long lRow = Long.parseLong(sRow);

				HotOrNot ex = new HotOrNot(this);
				ex.open();
				ex.updateEntry(lRow, mName, mHotness);
				ex.close();
			} catch(Exception e){
				String error = e.toString();
				Dialog d = new Dialog(this);
				d.setTitle("Dang it");
				TextView tv = new TextView(this);
				tv.setText(error);
				d.setContentView(tv);
				d.show();
			}
			break;

		// delete
		case R.id.bSQLdelete:
			try{
				String sRow1 = sqlRow.getText().toString();
				Long lRow1 = Long.parseLong(sRow1);

				HotOrNot ex1 = new HotOrNot(this);
				ex1.open();
				ex1.deleteEntry(lRow1);
				ex1.close();
			} catch(Exception e){
				String error = e.toString();
				Dialog d = new Dialog(this);
				d.setTitle("Dang it");
				TextView tv = new TextView(this);
				tv.setText(error);
				d.setContentView(tv);
				d.show();
			}
			break;
		}
	}
}



~
~<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name" />
    <EditText
        android:id="@+id/etSQLName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </EditText>
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hotness scale 1 to 10" />
    <EditText
        android:id="@+id/etSQLHotness"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/bSQLUpdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Update SQLite Database" />
    <Button
        android:id="@+id/bSQLopenView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="View" />
    <TextView
        android:id="@+id/TextView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Row ID" />
    <EditText
        android:id="@+id/etSQLrowInfo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </EditText>
    <Button
        android:id="@+id/bGetInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get Information" />
    <Button
        android:id="@+id/bSQLmodify"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Edit Entry" />
    <Button
        android:id="@+id/bSQLdelete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Delete Entry" />
~</LinearLayout>