Hatena::ブログ(Diary)

明日の鍵

2010-07-04

コールバックを使ってDialogの実装をスリムにする

edit_name_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <EditText 
    android:id="@+id/txtName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
  <LinearLayout
    android:id="@+id/footer"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <Button
      android:id="@+id/btnOK"
      android:text="@android:string/ok"
      android:layout_weight="1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" />
    <Button
      android:id="@+id/btnCancel"
      android:text="@android:string/cancel"
      android:layout_weight="1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" />
  </LinearLayout>
</LinearLayout>
EditNameDialog.java
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class EditNameDialog extends Dialog implements OnClickListener {

  private EditableListViewItem editableListViewItem;
  private int position;
  private String name;

  private EditText txtName;
  private Button btnOK;
  private Button btnCancel;

  public EditNameDialog(Context context, int position, String name, EditableListViewItem editableListViewItem) {
    super(context);
    this.editableListViewItem = editableListViewItem;
    this.name = name;
    this.position = position;
  }

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

    setTitle("edit name");

    txtName = (EditText) findViewById(R.id.txtName);
    txtName.setText(name);
    btnOK = (Button) findViewById(R.id.btnOK);
    btnCancel = (Button) findViewById(R.id.btnCancel);

    btnOK.setOnClickListener(this);
    btnCancel.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
    int id = v.getId();
    String name = txtName.getText().toString();
    if (id == R.id.btnOK) {
      if (editableListViewItem != null) {
        editableListViewItem.performEditName(position, name);
      }
      dismiss();
    } else {
      cancel();
    }
  }

  interface EditableListViewItem {
    void performEditName(int position, String name);
  }
}
Activity02.java
import java.util.ArrayList;

import jp.tomorrowkey.android.dialogsample01.EditNameDialog.EditableListViewItem;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemLongClickListener;

public class Activity02 extends ListActivity implements OnItemLongClickListener {
  private ListView list;
  private ArrayAdapter<String> adapter;
  private ArrayList<String> items;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    items = new ArrayList<String>();
    items.add("A");
    items.add("B");
    items.add("C");
    items.add("D");
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
    setListAdapter(adapter);

    list = (ListView) findViewById(android.R.id.list);
    list.setOnItemLongClickListener(this);
  }

  @Override
  public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
    String name = items.get(position);
    EditNameDialog dialog = new EditNameDialog(this, position, name, new EditableListViewItem() {
      @Override
      public void performEditName(int position, String name) {
        items.remove(position);
        items.add(position, name);
        adapter.notifyDataSetChanged();
      }
    });
    dialog.show();
    return false;
  }
}