mainActivity.java 에 추가
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10001) {
if (resultCode == SUCCESS) {
((TextView) findViewById(R.id.tv_selected_phone)).setText(data
.getStringExtra(SELECTED_PHONE));
} else {
((TextView) findViewById(R.id.tv_selected_phone)).setText("");
}
}
}
private void showContactlist() {
Intent intent = new Intent(MainActivity.this, ContactListActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivityForResult(intent, 10001);
}
ContactListActivity.java 생성
public class ContactListActivity extends Activity {
private ListView lv_contactlist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contactlist);
lv_contactlist = (ListView) findViewById(R.id.lv_contactlist);
}
@Override
protected void onResume() {
super.onResume();
ContactsAdapter adapter = new ContactsAdapter(ContactListActivity.this,
R.layout.layout_phonelist, getContactList());
lv_contactlist.setAdapter(adapter);
lv_contactlist
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> contactlist, View v,
int position, long resid) {
Contact phonenumber = (Contact) contactlist
.getItemAtPosition(position);
if (phonenumber == null) {
return;
}
Intent data = new Intent();
data.putExtra(MainActivity.SELECTED_PHONE, phonenumber
.getPhonenum().replaceAll("-", ""));
setResult(MainActivity.SUCCESS, data);
finish();
}
});
}
private ArrayList<Contact> getContactList() {
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME };
String[] selectionArgs = null;
String sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
Cursor contactCursor = managedQuery(uri, projection, null,
selectionArgs, sortOrder);
ArrayList<Contact> contactlist = new ArrayList<Contact>();
if (contactCursor.moveToFirst()) {
do {
String phonenumber = contactCursor.getString(1).replaceAll("-",
"");
if (phonenumber.length() == 10) {
phonenumber = phonenumber.substring(0, 3) + "-"
+ phonenumber.substring(3, 6) + "-"
+ phonenumber.substring(6);
} else if (phonenumber.length() > 8) {
phonenumber = phonenumber.substring(0, 3) + "-"
+ phonenumber.substring(3, 7) + "-"
+ phonenumber.substring(7);
}
Contact acontact = new Contact();
acontact.setPhotoid(contactCursor.getLong(0));
acontact.setPhonenum(phonenumber);
acontact.setName(contactCursor.getString(2));
contactlist.add(acontact);
} while (contactCursor.moveToNext());
}
return contactlist;
}
private class ContactsAdapter extends ArrayAdapter<Contact> {
private int resId;
private ArrayList<Contact> contactlist;
private LayoutInflater Inflater;
private Context context;
public ContactsAdapter(Context context, int textViewResourceId,
List<Contact> objects) {
super(context, textViewResourceId, objects);
this.context = context;
resId = textViewResourceId;
contactlist = (ArrayList<Contact>) objects;
Inflater = (LayoutInflater) ((Activity) context)
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View v, ViewGroup parent) {
ViewHolder holder;
if (v == null) {
v = Inflater.inflate(resId, null);
holder = new ViewHolder();
holder.tv_name = (TextView) v.findViewById(R.id.tv_name);
holder.tv_phonenumber = (TextView) v
.findViewById(R.id.tv_phonenumber);
holder.iv_photoid = (ImageView) v.findViewById(R.id.iv_photo);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
Contact acontact = contactlist.get(position);
if (acontact != null) {
holder.tv_name.setText(acontact.getName());
holder.tv_phonenumber.setText(acontact.getPhonenum());
Bitmap bm = openPhoto(acontact.getPhotoid());
if (bm != null) {
holder.iv_photoid.setImageBitmap(bm);
} else {
holder.iv_photoid.setImageDrawable(getResources()
.getDrawable(R.drawable.ic_launcher));
}
}
return v;
}
private Bitmap openPhoto(long contactId) {
Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI,
contactId);
InputStream input = ContactsContract.Contacts
.openContactPhotoInputStream(context.getContentResolver(),
contactUri);
if (input != null) {
return BitmapFactory.decodeStream(input);
}
return null;
}
private class ViewHolder {
ImageView iv_photoid;
TextView tv_name;
TextView tv_phonenumber;
}
}
}
Contact.Java 생성
public class Contact {
long photoid;
String phonenum;
String name;
public long getPhotoid() {
return photoid;
}
public void setPhotoid(long photoid) {
this.photoid = photoid;
}
public String getPhonenum() {
return phonenum;
}
public void setPhonenum(String phonenum) {
this.phonenum = phonenum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
activity_contactlist.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lv_contactlist"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
layout_phonelist.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="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"
android:gravity="center_vertical"
android:weightSum="10" >
<ImageView
android:id="@+id/iv_photo"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitXY" />
<TextView
android:id="@+id/tv_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:singleLine="true"
android:text="아무개"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_phonenumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="6"
android:singleLine="true"
android:text="000-0000-0000"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
'Programming > Android' 카테고리의 다른 글
다중 액티비티 예제 (0) | 2013.03.23 |
---|---|
안드로이드 레이아웃 예제 (0) | 2013.03.16 |
Intent 활용하기 (0) | 2013.03.08 |
ubuntu 에 jdk 간편하게 설치하기 (0) | 2013.02.27 |
R.java import 에러 (0) | 2012.11.27 |