主Activity
package com.rrr.providers;
import com.rrr.addresslist.AddPerson;
import com.rrr.addresslist.DBHelper;
import com.rrr.addresslist.DBOpenhelper;
import com.rrr.addresslist.People;
import com.rrr.addresslist.R;
import com.rrr.addresslist.SearchOne;
import com.rrr.addresslist.ShowItem;
import com.rrr.addresslist.R.id;
import com.rrr.addresslist.R.layout;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.Contacts.Intents.Insert;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class AddressListActivity extends Activity {
private ListView listview;
SimpleCursorAdapter adapter;
private ImageView imageview;
private TextView show;
private Intent i;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
listview = (ListView) findViewById(R.id.listViewMain);
imageview = (ImageView) findViewById(R.id.showimage);
show=(TextView) findViewById(R.id.textNumberOfPeople);
refresh();
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Cursor cursor=(Cursor) parent.getAdapter().getItem(position);
int currentid=cursor.getInt(0);
int quaryPhoto = cursor.getInt(1);
String quaryName=cursor.getString(2);
String quaryHome=cursor.getString(3);
String quaryPrivate=cursor.getString(4);
String quaryEmail=cursor.getString(5);
i=new Intent(AddressListActivity.this, ShowItem.class);
i.putExtra("currentid", currentid);
i.putExtra("pho", quaryPhoto);
i.putExtra("nam", quaryName);
i.putExtra("hom", quaryHome);
i.putExtra("pri", quaryPrivate);
i.putExtra("ema", quaryEmail);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, Menu.FIRST + 1, 1, "New").setIcon(android.R.drawable.ic_menu_add);
menu.add(Menu.NONE, Menu.FIRST + 2, 2, "Search").setIcon(android.R.drawable.ic_menu_search);
menu.add(Menu.NONE, Menu.FIRST + 3, 3, "Exit").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Menu.FIRST + 1:
i = new Intent(getApplicationContext(), AddPerson.class);
startActivityForResult(i, 0);
break;
case Menu.FIRST + 2:
i = new Intent(AddressListActivity.this, SearchOne.class);
startActivity(i);
break;
case Menu.FIRST + 3:
finish();
break;
}
return true;
}
public void refresh() {
//查询全部
Cursor cursor= getContentResolver().query(DBHelper.CONTENT_URI, null, null, null, null);
for(int i=0;i<cursor.getColumnCount();i++) {
System.out.println(cursor.getColumnName(i));
}
System.out.println("dsfsfd" + cursor.getColumnCount());
if (cursor == null || cursor.getCount() == 0) {
Toast.makeText(AddressListActivity.this, "当前没有记录!", 1000).show();
} else {
adapter = new SimpleCursorAdapter(AddressListActivity.this,
R.layout.itemxml, cursor, new String[] { DBHelper.PICTURE,
DBHelper.NAME }, new int[] { R.id.showimage,
R.id.showname });
listview.setAdapter(adapter);
show.setText("共有"+listview.getCount()+"个联系人");
}
}
//掉用生命周期来刷新
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
refresh();
}
}
Android通讯录
以下为几个基本界面的截图
图1 为主界面每次回来自动刷新,每一项item有单机事件,进入图3.
图3 可以给该联系人打电话,发短信,发邮件
图1 Menu键 new添加进入图2,选择头像进入图5
图1 Menu键 search可以按姓名查找联系人
图3 Menu键,deleat可以删除该用户,Edit可以对该用户进行修改
图1 图2
图3 图4
图5
AddPerson类
package com.rrr.addresslist;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.rrr.providers.AddressListActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView.ScaleType;
import android.widget.ViewSwitcher.ViewFactory;
public class AddPerson extends Activity implements OnClickListener{
private EditText EDname,EDhnumber,EDpnumber,EDemail;
private ImageView IBphoto;
private Button sure,cancle;
DBHelper helper;
private ImageSwitcher imgSwitcher;
private Gallery gallery;
private AlertDialog dialog;
private View view;
private int currentposition;
private int[]images={R.drawable.image1,R.drawable.image2,R.drawable.image3,
R.drawable.image4,R.drawable.image5,R.drawable.image6,R.drawable.image7,R.drawable.image8,
R.drawable.image9,R.drawable.image10,R.drawable.image11,R.drawable.image12,R.drawable.image13
,R.drawable.image14,R.drawable.image15,R.drawable.image16,R.drawable.image17,R.drawable.image18
,R.drawable.image19,R.drawable.image20,R.drawable.image21,R.drawable.image22,R.drawable.image23,
R.drawable.image24,R.drawable.image25,R.drawable.image26,R.drawable.image27,R.drawable.image28,
R.drawable.image29,R.drawable.image30};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.addperson);
EDname=(EditText) findViewById(R.id.EdAddNane);
EDhnumber=(EditText) findViewById(R.id.EdAddHomeNumber);
EDpnumber=(EditText) findViewById(R.id.EdAddPrivateNumber);
EDemail=(EditText) findViewById(R.id.EdAddEmail);
IBphoto=(ImageView) findViewById(R.id.imageButton1);
sure=(Button) findViewById(R.id.btnAdd);
cancle=(Button) findViewById(R.id.btnResect);
helper=new DBHelper();
sure.setOnClickListener(this);
cancle.setOnClickListener(this);
IBphoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
initView(); // 初始化Dialog上的View
initDialog();
dialog.show();
}
});
}
private void initView() {
if(view == null) {
// 把AlertDialog中显示控件加在了layout中
view = getLayoutInflater().inflate(R.layout.imagechoose, null);
gallery = (Gallery) view.findViewById(R.id.gallery);
// 加载适配器
gallery.setAdapter(new ImageAdapter(AddPerson.this, images));
gallery.setSelection(images.length / 2);
imgSwitcher = (ImageSwitcher)view.findViewById(R.id.imgswitcher);
imgSwitcher.setFactory(new ViewFactory() {
@Override
public View makeView() {
ImageView img = new ImageView(AddPerson.this);
// 设置img的参数
img.setLayoutParams(new ImageSwitcher.LayoutParams(100 , 120));
img.setScaleType(ScaleType.FIT_XY);
return img;
}
}) ;
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// 为ImageSwitcher设置图片
imgSwitcher.setImageResource(images[position%images.length]) ;
currentposition = position;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}) ;
}
}
private void initDialog() {
if(dialog == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(AddPerson.this);
builder.setTitle("Choose One Image : ");
builder.setView(view);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
IBphoto.setImageResource(images[currentposition % images.length]);
}
}) ;
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialog = builder.create();
}
}
@Override
public void onClick(View v) {
String name=EDname.getText().toString();
String hnum=EDhnumber.getText().toString();
String pnum=EDpnumber.getText().toString();
String email=EDemail.getText().toString();
int photo=images[currentposition % images.length];
switch(v.getId()){
case R.id.btnAdd :
People people=new People(photo,name, hnum, pnum, email);
//插入数据
ContentValues values = new ContentValues();
values.put(DBHelper.PICTURE, people.getPicture());
values.put(DBHelper.NAME, people.getname());
values.put(DBHelper.HOMEPHONE, people.gethomePhone());
values.put(DBHelper.PRIVATEPHONE, people.getprivatePhone());
values.put(DBHelper.EMAIL, people.getEmail());
getContentResolver().insert(DBHelper.CONTENT_URI, values);
Toast.makeText(AddPerson.this, photo+"添加成功", 1000).show();
break;
case R.id.btnResect:
EDname.setText("");
EDhnumber.setText("");
EDpnumber.setText("");
EDemail.setText("");
IBphoto.setImageResource(R.drawable.icon);
break;
}
}
@Override
protected void onDestroy() {
setResult(RESULT_OK);
super.onDestroy();
}
}
//适配器
class ImageAdapter extends BaseAdapter {
private Context context;
private int[] images;
public ImageAdapter(Context context , int[] img) {
this.context = context;
this.images = img;
}
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return images[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView view = new ImageView(context);
view.setLayoutParams(new Gallery.LayoutParams(80 , 110));
view.setScaleType(ScaleType.FIT_XY);
view.setAdjustViewBounds(true); // 自调整边界
view.setImageResource(images[position%images.length]);
return view;
}
}
People类
package com.rrr.addresslist;
public class People {
private int id;
private int picture;
private String name;
private String homePhone;
private String privatePhone;
private String email;
public People(int picture,String name,String homePhone,String privatePhone,String email){
this.picture=picture;
this.name=name;
this.homePhone=homePhone;
this.privatePhone=privatePhone;
this.email=email;
}
public People(int picture,String name){
this.picture=picture;
this.name=name;
}
public int getId() {
return id;
}
public int getPicture() {
return picture;
}
public void setPicture(int picture) {
this.picture = picture;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void setId(int id) {
this.id = id;
}
public String getname() {
return name;
}
public void setname(String name) {
this.name = name;
}
public String gethomePhone() {
return homePhone;
}
public void sethomePhone(String homePhone) {
this.homePhone = homePhone;
}
public String getprivatePhone() {
return privatePhone;
}
public void setprivatePhone(String privatePhone) {
this.privatePhone = privatePhone;
}
}
SearchOne 类
package com.rrr.addresslist;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class SearchOne extends Activity{
private EditText searchone;
private ImageButton click;
private DBHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.searchone);
searchone=(EditText) findViewById(R.id.EditSearch);
click=(ImageButton) findViewById(R.id.imageButtonSearch);
helper=new DBHelper();
click.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String searchname=searchone.getText().toString();
String s="name like"+ ("'"+searchname+"'").trim();
//查询单行
Cursor cursor=getContentResolver().query(DBHelper.CONTENT_URI,null,s,null,null);
if(cursor.getColumnCount()<=0){
Toast.makeText(SearchOne.this, "没有该联系人!", 1000).show();
}
else{
while(cursor.moveToNext()) {
int id=cursor.getInt(0);
int quaryPhoto = cursor.getInt(1);
String quaryName=cursor.getString(2);
String quaryHome=cursor.getString(3);
String quaryPrivate=cursor.getString(4);
String quaryEmail=cursor.getString(5);
Intent i=new Intent(SearchOne.this, ShowOne.class);
//传id
i.putExtra("id", id);
i.putExtra("pho", quaryPhoto);
i.putExtra("nam", quaryName);
i.putExtra("hom", quaryHome);
i.putExtra("pri", quaryPrivate);
i.putExtra("ema", quaryEmail);
startActivity(i);
}
}
}
});
}
}
ShowItem 类
package com.rrr.addresslist;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class SearchOne extends Activity{
private EditText searchone;
private ImageButton click;
private DBHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.searchone);
searchone=(EditText) findViewById(R.id.EditSearch);
click=(ImageButton) findViewById(R.id.imageButtonSearch);
helper=new DBHelper();
click.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String searchname=searchone.getText().toString();
String s="name like"+ ("'"+searchname+"'").trim();
//查询单行
Cursor cursor=getContentResolver().query(DBHelper.CONTENT_URI,null,s,null,null);
if(cursor.getColumnCount()<=0){
Toast.makeText(SearchOne.this, "没有该联系人!", 1000).show();
}
else{
while(cursor.moveToNext()) {
int id=cursor.getInt(0);
int quaryPhoto = cursor.getInt(1);
String quaryName=cursor.getString(2);
String quaryHome=cursor.getString(3);
String quaryPrivate=cursor.getString(4);
String quaryEmail=cursor.getString(5);
Intent i=new Intent(SearchOne.this, ShowOne.class);
//传id
i.putExtra("id", id);
i.putExtra("pho", quaryPhoto);
i.putExtra("nam", quaryName);
i.putExtra("hom", quaryHome);
i.putExtra("pri", quaryPrivate);
i.putExtra("ema", quaryEmail);
startActivity(i);
}
}
}
});
}
}
ShowOne 类
package com.rrr.addresslist;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class SearchOne extends Activity{
private EditText searchone;
private ImageButton click;
private DBHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.searchone);
searchone=(EditText) findViewById(R.id.EditSearch);
click=(ImageButton) findViewById(R.id.imageButtonSearch);
helper=new DBHelper();
click.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String searchname=searchone.getText().toString();
String s="name like"+ ("'"+searchname+"'").trim();
//查询单行
Cursor cursor=getContentResolver().query(DBHelper.CONTENT_URI,null,s,null,null);
if(cursor.getColumnCount()<=0){
Toast.makeText(SearchOne.this, "没有该联系人!", 1000).show();
}
else{
while(cursor.moveToNext()) {
int id=cursor.getInt(0);
int quaryPhoto = cursor.getInt(1);
String quaryName=cursor.getString(2);
String quaryHome=cursor.getString(3);
String quaryPrivate=cursor.getString(4);
String quaryEmail=cursor.getString(5);
Intent i=new Intent(SearchOne.this, ShowOne.class);
//传id
i.putExtra("id", id);
i.putExtra("pho", quaryPhoto);
i.putExtra("nam", quaryName);
i.putExtra("hom", quaryHome);
i.putExtra("pri", quaryPrivate);
i.putExtra("ema", quaryEmail);
startActivity(i);
}
}
}
});
}
}
EditContent 类
package com.rrr.addresslist;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView.ScaleType;
import android.widget.ViewSwitcher.ViewFactory;
public class EditContent extends Activity implements OnClickListener{
private EditText EDname,EDhnumber,EDpnumber,EDemail;
private ImageView IBphoto;
private Button change,reset;
private DBHelper helper;
int table_id,pho;
Intent intent;
String s,nam,pri,hom,ema;
private ImageSwitcher imgSwitcher;
private Gallery gallery;
private AlertDialog dialog;
private View view;
private int currentposition;
private int[]images={R.drawable.image1,R.drawable.image2,R.drawable.image3,
R.drawable.image4,R.drawable.image5,R.drawable.image6,R.drawable.image7,R.drawable.image8,
R.drawable.image9,R.drawable.image10,R.drawable.image11,R.drawable.image12,R.drawable.image13
,R.drawable.image14,R.drawable.image15,R.drawable.image16,R.drawable.image17,R.drawable.image18
,R.drawable.image19,R.drawable.image20,R.drawable.image21,R.drawable.image22,R.drawable.image23,
R.drawable.image24,R.drawable.image25,R.drawable.image26,R.drawable.image27,R.drawable.image28,
R.drawable.image29,R.drawable.image30};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.editcontent);
EDname=(EditText) findViewById(R.id.EdAddNane_E);
EDhnumber=(EditText) findViewById(R.id.EdAddHomeNumber_E);
EDpnumber=(EditText) findViewById(R.id.EdAddPrivateNumber_E);
EDemail=(EditText) findViewById(R.id.EdAddEmail_E);
IBphoto=(ImageView) findViewById(R.id.imageButton_E);
change=(Button) findViewById(R.id.btnAdd_E);
reset=(Button) findViewById(R.id.btnResect_E);
helper=new DBHelper();
change.setOnClickListener(this);
reset.setOnClickListener(this);
intent=getIntent();
table_id= intent.getIntExtra("id", -4);
System.out.println(table_id);
s=" _ID="+table_id;
pho=intent.getIntExtra("pho", -6);
System.out.println(pho);
nam=intent.getStringExtra("nam");
pri=intent.getStringExtra("pri");
hom=intent.getStringExtra("hom");
ema=intent.getStringExtra("ema");
IBphoto.setImageResource(pho);
EDname.setText(nam);
EDhnumber.setText(hom);
EDpnumber.setText(pri);
EDemail.setText(ema);
IBphoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
initView(); // 初始化Dialog上的View
initDialog();
dialog.show();
}
});
}
private void initView() {
if(view == null) {
// 把AlertDialog中显示控件加在了layout中
view = getLayoutInflater().inflate(R.layout.imagechoose, null);
gallery = (Gallery) view.findViewById(R.id.gallery);
// 加载适配器
gallery.setAdapter(new ImageAdapter2(EditContent.this, images));
gallery.setSelection(images.length / 2);
imgSwitcher = (ImageSwitcher)view.findViewById(R.id.imgswitcher);
imgSwitcher.setFactory(new ViewFactory() {
@Override
public View makeView() {
ImageView img = new ImageView(EditContent.this);
// 设置img的参数
img.setLayoutParams(new ImageSwitcher.LayoutParams(100 , 120));
img.setScaleType(ScaleType.FIT_XY);
return img;
}
}) ;
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// 为ImageSwitcher设置图片
imgSwitcher.setImageResource(images[position%images.length]) ;
currentposition = position;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}) ;
}
}
private void initDialog() {
if(dialog == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(EditContent.this);
builder.setTitle("Choose One Image : ");
builder.setView(view);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
IBphoto.setImageResource(images[currentposition % images.length]);
}
}) ;
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialog = builder.create();
}
}
@Override
public void onClick(View v) {
String name=EDname.getText().toString();
String hnum=EDhnumber.getText().toString();
String pnum=EDpnumber.getText().toString();
String email=EDemail.getText().toString();
int photo=images[currentposition % images.length];
switch(v.getId()){
case R.id.btnAdd_E :
//更新
People people=new People(photo,name, hnum, pnum, email);
ContentValues values = new ContentValues();
values.put(DBHelper.PICTURE, people.getPicture());
values.put(DBHelper.NAME, people.getname());
values.put(DBHelper.HOMEPHONE, people.gethomePhone());
values.put(DBHelper.PRIVATEPHONE, people.getprivatePhone());
values.put(DBHelper.EMAIL, people.getEmail());
getContentResolver().update(DBHelper.CONTENT_URI, values, s, null);
Toast.makeText(EditContent.this, photo+","+name+","+hnum+"修改成功"+s, 1000).show();
break;
case R.id.btnResect_E :
IBphoto.setImageResource(pho);
EDname.setText(nam);
EDhnumber.setText(hom);
EDpnumber.setText(pri);
EDemail.setText(ema);
break;
}
}
}
class ImageAdapter2 extends BaseAdapter {
private Context context;
private int[] images;
public ImageAdapter2(Context context , int[] img) {
this.context = context;
this.images = img;
}
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return images[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView view = new ImageView(context);
view.setLayoutParams(new Gallery.LayoutParams(80 , 110));
view.setScaleType(ScaleType.FIT_XY);
view.setAdjustViewBounds(true); // 自调整边界
view.setImageResource(images[position%images.length]);
return view;
}
}
DBHelper 类
package com.rrr.addresslist;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class DBHelper extends ContentProvider{
public static final String _ID = "_id";
public static final String PICTURE= "picture";
public static final String NAME = "name";
public static final String HOMEPHONE = "homephone";
public static final String PRIVATEPHONE="privatephone";
public static final String EMAIL="email";
public static final String TABLE_NAME = "t_address";
public static final String CREATE_TABLE =
"CREATE TABLE "+ TABLE_NAME +"(" + _ID + " integer primary key autoincrement,"
+PICTURE+" int,"+NAME+" text,"+HOMEPHONE+ " text,"+PRIVATEPHONE+ " text,"+EMAIL+" text);";
public static final String AUTHORITY = "com.rrr.providers.dbhelper";
// 提供给外部的Uri
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/people");
private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final int PEOPLE_ALL_FLAG = 1;
private static final int PEOPLE_ITEM_FLAG = 2;
static {
uriMatcher.addURI(AUTHORITY, "people", PEOPLE_ALL_FLAG);
uriMatcher.addURI(AUTHORITY, "people/#", PEOPLE_ITEM_FLAG);
}
private DBOpenhelper openHelper;
@Override
public boolean onCreate() {
openHelper=new DBOpenhelper(this.getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = openHelper.getReadableDatabase();
switch(uriMatcher.match(uri)) {
case PEOPLE_ALL_FLAG : // 查询所有数据
return db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
case PEOPLE_ITEM_FLAG : // 根据id查询数据
// 得到末尾的id
long id = ContentUris.parseId(uri);
String where = _ID + "=" + id ; // 构造id的where
// 判断参数的where 部分是否存在
if(selection != null && selection.trim().length() != 0) {
where = where + " AND " + selection;
}
return db.query(TABLE_NAME, projection, where, selectionArgs, null, null, sortOrder);
default :
throw new IllegalArgumentException("未知的Uri : " + uri);
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = openHelper.getWritableDatabase();
long rowId = db.insert(TABLE_NAME, _ID, values);
getContext().getContentResolver().notifyChange(uri, null);
db.close();
return ContentUris.withAppendedId(uri, rowId);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = openHelper.getReadableDatabase();
//删除的记录数
int num=0;
switch(uriMatcher.match(uri)) {
case PEOPLE_ALL_FLAG : // 查询所有数据
return db.delete(TABLE_NAME, selection,selectionArgs );
case PEOPLE_ITEM_FLAG : // 根据id查询数据
// 得到末尾的id
long id = ContentUris.parseId(uri);
String where = _ID + "=" + id ; // 构造id的where
// 判断参数的where 部分是否存在
if(selection != null && !selection.trim().equals("")) {
//如果原来的where句子存在拼接where句子
where = where + " AND " + selection;
}
return num=db.delete(TABLE_NAME, where, selectionArgs) ;
default :
throw new IllegalArgumentException("未知的Uri : " + uri);
}
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int num=0;
SQLiteDatabase db = openHelper.getWritableDatabase();
switch(uriMatcher.match(uri))
{
case PEOPLE_ALL_FLAG : // 查询所有数据
return db.update(TABLE_NAME, values, selection, selectionArgs);
case PEOPLE_ITEM_FLAG : // 根据id查询数据
// 得到末尾的id
long id = ContentUris.parseId(uri);
String where = _ID + "=" + id ; // 构造id的where
// 判断参数的where 部分是否存在
if(selection != null && !selection.trim().equals("")) {
//如果原来的where句子存在拼接where句子
where = where + " AND " + selection;
}
num=db.update(TABLE_NAME, values, selection, selectionArgs);
break;
default :
throw new IllegalArgumentException("未知的Uri : " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return num;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
}
DBOpenHelper 类
package com.rrr.addresslist;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DBOpenhelper extends SQLiteOpenHelper{
// 定义列名常量
private static final int DATABASE_VERSION = 3;
private static final String DATABASE_NAME = "db_addresslist" ;
public DBOpenhelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DBHelper.CREATE_TABLE) ;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}