主界面MainActivity代码:
import android.app.Activity; import android.app.ListActivity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.SimpleCursorAdapter; import android.widget.TextView; public class MainActivity extends ListActivity { private static final int ITEM_ADD=1; private DBLocalService db; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); db=new DBLocalService(this); TextView text1=(TextView) findViewById(R.id.text1); Cursor cursor=db.querysql("select * from "+db.TABLE_CONTACTS,null); SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter( this,android.R.layout.simple_expandable_list_item_2,cursor,new String[]{"name","tel" },new int[]{android.R.id.text1,android.R.id.text2}); setListAdapter(simpleCursorAdapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuItem addMenuItem=menu.add(1,ITEM_ADD,3,"添加联系人"); addMenuItem.setIcon(android.R.drawable.ic_menu_add); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()){ case ITEM_ADD:{ Intent intent=new Intent(); intent.setClass(this,AddContactActivity.class); startActivity(intent); return true; } } return true; } }
项目工程代码:http://good.gd/1074611.htm