package com.android.homework; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.app.AlertDialog; import android.app.DatePickerDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.ListView; import android.widget.SimpleAdapter; public class AddMessage extends Activity { private ArrayList<HashMap<String, String>> mylist; private SimpleAdapter myListAdapter; private ListView list; public static final int EDITMENU = 0; public static final int REMOVEMENU = 1; public static final int ADDMENU = 2; public static final int ADD_DIALOG_ID = 3; public static final int EDIT_DIALOG_ID = 4; private int myYear; private int myMonth; private int myDay; private Button dateButton; private EditText nameText, ageText; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myListViewInit(); } public void myListViewInit() { list = (ListView) findViewById(R.id.ListView); mylist = new ArrayList<HashMap<String, String>>(); HashMap<String, String> map = new HashMap<String, String>(); map.put("name", getString(R.string.name) + "peter"); map.put("age", getString(R.string.age) + "25"); map.put("birthday", getString(R.string.birthday) + "1986/08/30"); mylist.add(map); myListAdapter = new SimpleAdapter(this, mylist, R.layout.listview, new String[] { "name", "age", "birthday" }, new int[] { R.id.name, R.id.age, R.id.birthdaytext }); list.setAdapter(myListAdapter); setOnClickListener(); } public void setOnClickListener() { this.registerForContextMenu(list); list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { AddMessage.this.openContextMenu(arg1); } }); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // TODO Auto-generated method stub super.onCreateContextMenu(menu, v, menuInfo); menu.add(0, EDITMENU, 0, "Edit"); menu.add(0, REMOVEMENU, 1, "Remove"); } public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, ADDMENU, 0, "Add"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub addmessage(ADD_DIALOG_ID); return super.onOptionsItemSelected(item); } @Override public boolean onContextItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case EDITMENU: AdapterContextMenuInfo infoEdit = (AdapterContextMenuInfo) item .getMenuInfo(); editmessage(EDIT_DIALOG_ID, infoEdit); break; case REMOVEMENU: AdapterContextMenuInfo infoRemove = (AdapterContextMenuInfo) item .getMenuInfo(); myListViewRemove(infoRemove); break; default: break; } return super.onContextItemSelected(item); } private void addmessage(int id) { // TODO Auto-generated method stub LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.dialog1, (ViewGroup) findViewById(R.id.dialog)); dateButton = (Button) layout.findViewById(R.id.birthdaybutton); nameText = (EditText) layout.findViewById(R.id.name); ageText = (EditText) layout.findViewById(R.id.age); dateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub datePickerChange(v); } }); new AlertDialog.Builder(this).setView(layout).setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { myListViewAdd(nameText.getText().toString(), ageText.getText().toString(), dateButton.getText().toString()); } }).setNegativeButton("Cancel", null).show(); } protected void datePickerChange(View v) { new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub myYear = year; myMonth = monthOfYear; myDay = dayOfMonth; dateButton.setText(new StringBuilder().append(myYear).append("/") .append(myMonth + 1).append("/") .append(myDay)); } }, 1986, 8, 30).show(); } private void editmessage(int id, final AdapterContextMenuInfo info) { LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.dialog1, (ViewGroup) findViewById(R.id.dialog)); dateButton = (Button) layout.findViewById(R.id.birthdaybutton); nameText = (EditText) layout.findViewById(R.id.name); ageText = (EditText) layout.findViewById(R.id.age); String nameBefore = (String) mylist.get(info.position).get("name").substring(5); String ageBefore = mylist.get(info.position).get("age").substring(4); nameText.setText(nameBefore); ageText.setText(ageBefore); dateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { datePickerChange(v); } }); new AlertDialog.Builder(this).setView(layout).setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { HashMap<String, String> map = new HashMap<String, String>(); map.put("name", getString(R.string.name) + nameText.getText()); map.put("age", getString(R.string.age) + ageText.getText()); map.put("birthday", getString(R.string.birthday) + dateButton.getText()); mylist.set(info.position, map); myListAdapter.notifyDataSetChanged(); } }).setNegativeButton("Cancel", null).show(); } // add a list in the ListView protected void myListViewAdd(String name, String age, String birthday) { HashMap<String, String> map = new HashMap<String, String>(); map.put("name", getString(R.string.name) + name); map.put("age", getString(R.string.age) + age); map.put("birthday", getString(R.string.birthday) + birthday); mylist.add(map); myListAdapter = new SimpleAdapter(this, mylist, R.layout.listview, new String[] { "name", "age", "birthday" }, new int[] { R.id.name, R.id.age, R.id.birthdaytext }); list.setAdapter(myListAdapter); myListAdapter.notifyDataSetChanged(); } // remove the list you choose protected void myListViewRemove(AdapterContextMenuInfo infoRemove) { mylist.remove(infoRemove.position); myListAdapter.notifyDataSetChanged(); } }
以下是dialog.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ListView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#447788" android:textSize="15dp"/> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/age" android:layout_width="fill_parent" android:layout_weight="2" android:layout_height="wrap_content" android:textColor="#997722" android:textSize="15dp"/> <TextView android:id="@+id/birthdaytext" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="#772299" android:textSize="15dp"/> </LinearLayout> </LinearLayout>
listView.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ListView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#447788" android:textSize="15dp"/> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/age" android:layout_width="fill_parent" android:layout_weight="2" android:layout_height="wrap_content" android:textColor="#997722" android:textSize="15dp"/> <TextView android:id="@+id/birthdaytext" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="#772299" android:textSize="15dp"/> </LinearLayout> </LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+id/ListView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbars="vertical" /> </LinearLayout>
顯示一個ListView,每個Item顯示一個人的名字、年齡以及生日。
1. 按下menu鍵,會顯示一個”Add”選項讓使用者自行新增使用者項目
2. 點擊每個項目,會出現ContextMenu,共有兩個項目,”Edit”以及”Remove”
點選Add以及Edit選項,會彈跳出一個dialog讓使用者新增或修改項目。
點選Remove時,則將此項目從ListView移除掉。