編集画面
package com.example.contactlistsample;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class EditActivity extends Activity implements OnClickListener {
private MyOpenHelper helper = new MyOpenHelper(this);
private EditText nameEdit2;
private EditText numberEdit2;
private EditText addressEdit2;
private String tableName = "person";
private SQLiteDatabase db;
private Cursor c;
private long id;
private AlertDialog alertDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
db = helper.getWritableDatabase();
Intent intent = getIntent();
id = intent.getLongExtra("id", 0l);
c = db.query(tableName, new String[] { PersonTable.NAME,
PersonTable.NUMBER, PersonTable.ADDRESS }, "_id = ?",
new String[] { String.valueOf(id) }, null, null, null);
nameEdit2 = (EditText) findViewById(R.id.editName2);
numberEdit2 = (EditText) findViewById(R.id.editNumber2);
addressEdit2 = (EditText) findViewById(R.id.editAddress2);
c.moveToFirst();
nameEdit2.setText(c.getString(c.getColumnIndex(PersonTable.NAME)));
numberEdit2.setText(c.getString(c.getColumnIndex(PersonTable.NUMBER)));
addressEdit2
.setText(c.getString(c.getColumnIndex(PersonTable.ADDRESS)));
Button editB2 = (Button) findViewById(R.id.editButton2);
Button editB3 = (Button) findViewById(R.id.editButton3);
editB2.setOnClickListener(this);
editB3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.editButton2:
String name2 = nameEdit2.getText().toString();
String number2 = numberEdit2.getText().toString();
String address2 = addressEdit2.getText().toString();
if (name2.equals("")) {
Toast.makeText(this, "名前を入力してください。", Toast.LENGTH_SHORT).show();
} else if (number2.equals("")) {
Toast.makeText(this, "電話番号を入力してください。", Toast.LENGTH_SHORT)
.show();
} else {
ContentValues insert2 = new ContentValues();
insert2.put("name", name2);
insert2.put("number", number2);
insert2.put("address", address2);
db.update(tableName, insert2, "_id = ?",
new String[] { String.valueOf(id) });
}
nameEdit2.getEditableText().clear();
numberEdit2.getEditableText().clear();
Intent backIntent = new Intent(EditActivity.this,
MainActivity.class);
startActivity(backIntent);
break;
case R.id.editButton3:
Alert();
default:
break;
}
}
public void Alert() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("注意");
builder.setMessage("このデータを削除します。" + "n" + "よろしいですか?");
builder.setPositiveButton("削除", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
db.delete(tableName, "_id = ?",
new String[] { String.valueOf(id) });
Intent backIntent2 = new Intent(EditActivity.this,
MainActivity.class);
startActivity(backIntent2);
}
});
builder.setNegativeButton("キャンセル",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setCancelable(true);
alertDialog = builder.create();
alertDialog.show();
}
}
activity_edit.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="名前" />
<EditText
android:id="@+id/editName2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="電話番号" />
<EditText
android:id="@+id/editNumber2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="メールアドレス" />
<EditText
android:id="@+id/editAddress2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/editButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="上書き" />
<Button
android:id="@+id/editButton3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="削除" />
</LinearLayout>
</LinearLayout>
スプラッシュ画面
package com.example.contactlistsample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
public class SplashActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this,
MainActivity.class);
startActivity(intent);
}
}, 1500);
}
}
activity_splash
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:src="@drawable/splash"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
「データベース」の生成ファイル
package com.example.contactlistsample;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyOpenHelper extends SQLiteOpenHelper {
public MyOpenHelper(Context context) {
super(context, "NameAgeDB", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table person(" + " _id INTEGER PRIMARY KEY, "
+ PersonTable.NAME + " text not null," + PersonTable.NUMBER
+ " text not null," + PersonTable.ADDRESS + " text " + ");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
class PersonTable {
public static final String NAME = "name";
public static final String NUMBER = "number";
public static final String ADDRESS = "address";
}
Androidアプリ開発の必須知識!JAVAプログラミングを学べる連載リンク
はじめてのJAVA 連載