activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="9"
android:gravity="top"
android:orientation="horizontal" >
<ListView
android:id="@+id/ListView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_weight="1"
android:gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="@+id/entry"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_weight="1"
android:text="新規 " />
<Button
android:id="@+id/delete"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_weight="1"
android:text="データ削除" />
</LinearLayout>
</LinearLayout>
登録画面
package com.example.contactlistsample;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
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 EntryActivity extends Activity implements OnClickListener {
private MyOpenHelper helper = new MyOpenHelper(this);
private EditText nameEdit;
private EditText numberEdit;
private EditText addressEdit;
private String tableName = "person";
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_entry);
db = helper.getWritableDatabase();
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
nameEdit = (EditText) findViewById(R.id.editName);
numberEdit = (EditText) findViewById(R.id.editNumber);
addressEdit = (EditText) findViewById(R.id.editAddress);
String name = nameEdit.getText().toString();
String number = numberEdit.getText().toString();
String address = addressEdit.getText().toString();
if (name.equals("")) {
Toast.makeText(this, "名前を入力してください。", Toast.LENGTH_SHORT).show();
} else if (number.equals("")) {
Toast.makeText(this, "電話番号を入力してください。", Toast.LENGTH_SHORT).show();
} else {
ContentValues insert = new ContentValues();
insert.put("name", name);
insert.put("number", number);
insert.put("address", address);
db.insert(tableName, name, insert);
Toast.makeText(this, name + "さんを登録しました。", Toast.LENGTH_SHORT)
.show();
}
nameEdit.getEditableText().clear();
numberEdit.getEditableText().clear();
Intent backIntent = new Intent(EntryActivity.this, MainActivity.class);
startActivity(backIntent);
}
}
activity_entry.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/editName"
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/editNumber"
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/editAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登録" />
</LinearLayout>
項目の詳細画面
package com.example.contactlistsample;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class DetailActivity extends Activity implements OnClickListener {
private MyOpenHelper helper = new MyOpenHelper(this);
private TextView nameTextView;
private TextView numberTextView;
private TextView addressTextView;
private String tableName = "person";
private SQLiteDatabase db;
private long id;
private Cursor c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
db = helper.getReadableDatabase();
Intent intent = getIntent();
id = intent.getLongExtra("number", 0l);
c = db.query(tableName, new String[] { PersonTable.NAME,
PersonTable.NUMBER, PersonTable.ADDRESS }, "_id = ?",
new String[] { String.valueOf(id) }, null, null, null);
nameTextView = (TextView) findViewById(R.id.textName);
numberTextView = (TextView) findViewById(R.id.textNumber);
addressTextView = (TextView) findViewById(R.id.textAddress);
c.moveToFirst();
nameTextView.setText(c.getString(c.getColumnIndex(PersonTable.NAME)));
numberTextView
.setText(c.getString(c.getColumnIndex(PersonTable.NUMBER)));
addressTextView.setText(c.getString(c
.getColumnIndex(PersonTable.ADDRESS)));
Button editB = (Button) findViewById(R.id.editButton);
Button callB = (Button) findViewById(R.id.callButton);
Button mailB = (Button) findViewById(R.id.mailButton);
editB.setOnClickListener(this);
callB.setOnClickListener(this);
mailB.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.editButton:
Intent editIntent = new Intent(DetailActivity.this,
EditActivity.class);
editIntent.putExtra("id", id);
startActivity(editIntent);
break;
case R.id.callButton:
Uri uriCall = Uri.parse("tel:"
+ c.getString(c.getColumnIndex(PersonTable.NUMBER)));
Intent callIntent = new Intent(Intent.ACTION_CALL, uriCall);
startActivity(callIntent);
break;
case R.id.mailButton:
Intent mailIntent = new Intent(Intent.ACTION_SEND);
mailIntent.setType("text/plain");
mailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { c
.getString(c.getColumnIndex(PersonTable.ADDRESS)) });
startActivity(mailIntent);
break;
default:
break;
}
}
}
activity_detail.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"
android:padding="15dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="名前" />
<TextView
android:id="@+id/textName"
android:layout_width="match_parent"
android:layout_height="48dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="電話番号" />
<TextView
android:id="@+id/textNumber"
android:layout_width="match_parent"
android:layout_height="48dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="メールアドレス" />
<TextView
android:id="@+id/textAddress"
android:layout_width="match_parent"
android:layout_height="48dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/editButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="編集" />
<Button
android:id="@+id/callButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="電話" />
<Button
android:id="@+id/mailButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="メール" />
</LinearLayout>
</LinearLayout>
Androidアプリ開発の必須知識!JAVAプログラミングを学べる連載リンク
はじめてのJAVA 連載