001.
package
com.myapp;
002.
003.
import
java.io.InputStream;
004.
import
java.util.ArrayList;
005.
import
java.util.HashMap;
006.
007.
import
android.net.Uri;
008.
import
android.os.Bundle;
009.
import
android.provider.ContactsContract;
010.
import
android.view.LayoutInflater;
011.
import
android.view.Menu;
012.
import
android.view.View;
013.
import
android.view.ViewGroup;
014.
import
android.widget.AdapterView;
015.
import
android.widget.AdapterView.OnItemClickListener;
016.
import
android.widget.BaseAdapter;
017.
import
android.widget.ImageView;
018.
import
android.widget.ListView;
019.
import
android.widget.TextView;
020.
import
android.widget.Toast;
021.
import
android.app.Activity;
022.
import
android.content.ContentResolver;
023.
import
android.content.ContentUris;
024.
import
android.content.Context;
025.
import
android.database.Cursor;
026.
import
android.graphics.Bitmap;
027.
import
android.graphics.BitmapFactory;
028.
029.
public
class
MainActivity
extends
Activity {
030.
031.
ArrayList<HashMap<String, Object>> MyArrList;
032.
033.
@Override
034.
public
void
onCreate(Bundle savedInstanceState) {
035.
super
.onCreate(savedInstanceState);
036.
setContentView(R.layout.activity_main);
037.
038.
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
null
,
null
,
null
,
null
);
039.
startManagingCursor(cursor);
040.
041.
MyArrList =
new
ArrayList<HashMap<String, Object>>();
042.
HashMap<String, Object> map;
043.
044.
if
(cursor !=
null
)
045.
{
046.
if
(cursor.moveToFirst()) {
047.
048.
049.
map =
new
HashMap<String, Object>();
050.
051.
052.
053.
054.
055.
056.
057.
do
{
058.
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
059.
060.
map =
new
HashMap<String, Object>();
061.
062.
063.
map.put(
"ID"
, (String)cursor.getString( cursor.getColumnIndex(ContactsContract.Contacts._ID)));
064.
065.
066.
map.put(
"NAME"
, (String)cursor.getString( cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
067.
068.
069.
String tmpPhoneNo =
""
;
070.
if
(Integer.parseInt(cursor.getString(cursor.getColumnIndex( ContactsContract.Contacts.HAS_PHONE_NUMBER))) >
0
) {
071.
Cursor pCur = getContentResolver().query(
072.
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
073.
null
,
074.
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +
" = ?"
,
075.
new
String[]{id},
null
);
076.
077.
while
(pCur.moveToNext()) {
078.
String PhoneNo = pCur.getString(pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.DATA));
079.
if
(tmpPhoneNo !=
null
)
080.
{
081.
tmpPhoneNo = tmpPhoneNo +
", "
+ PhoneNo;
082.
}
083.
else
084.
{
085.
tmpPhoneNo = PhoneNo;
086.
}
087.
}
088.
pCur.close();
089.
090.
}
091.
092.
093.
map.put(
"PHONENO"
, (String)tmpPhoneNo);
094.
095.
096.
Bitmap tmpPhoto =
null
;
097.
final
ContentResolver resolver = getContentResolver();
098.
Cursor sCur = resolver.query(
099.
ContactsContract.Data.CONTENT_URI,
100.
null
,
101.
ContactsContract.Data.CONTACT_ID +
"="
+ id +
" AND "
102.
+ ContactsContract.Data.MIMETYPE +
"='"
103.
+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE +
"'"
,
null
,
104.
null
);
105.
if
(sCur ==
null
) {
106.
tmpPhoto =
null
;
107.
}
108.
else
109.
{
110.
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long
111.
.parseLong(id));
112.
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);
113.
if
(input ==
null
) {
114.
tmpPhoto =
null
;
115.
}
116.
else
117.
{
118.
tmpPhoto= BitmapFactory.decodeStream(input);
119.
}
120.
121.
}
122.
sCur.close();
123.
map.put(
"PHOTO"
, (Bitmap)tmpPhoto);
124.
125.
126.
MyArrList.add(map);
127.
128.
}
while
(cursor.moveToNext());
129.
130.
}
131.
}
132.
cursor.close();
133.
134.
final
ListView lView1 = (ListView)findViewById(R.id.listView1);
135.
lView1.setAdapter(
new
ImageAdapter(
this
));
136.
137.
138.
lView1.setOnItemClickListener(
new
OnItemClickListener() {
139.
public
void
onItemClick(AdapterView<?> parent, View v,
140.
int
position,
long
id) {
141.
142.
Toast.makeText(getApplicationContext(),
143.
"Your selected : "
+ MyArrList.get(position).get(
"ID"
), Toast.LENGTH_SHORT).show();
144.
145.
146.
/*
147.
*
148.
Intent intent =
new
Intent(Intent.ACTION_CALL);
149.
long
phoneId = Integer.parseInt(MyArrList.get(position).get(
"ID"
));
150.
intent.setData(ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, phoneId));
151.
startActivity(intent);
152.
*/
153.
154.
}
155.
});
156.
157.
}
158.
159.
public
class
ImageAdapter
extends
BaseAdapter
160.
{
161.
private
Context context;
162.
163.
public
ImageAdapter(Context c)
164.
{
165.
166.
context = c;
167.
}
168.
169.
public
int
getCount() {
170.
171.
return
MyArrList.size();
172.
}
173.
174.
public
Object getItem(
int
position) {
175.
176.
return
position;
177.
}
178.
179.
public
long
getItemId(
int
position) {
180.
181.
return
position;
182.
}
183.
184.
public
View getView(
int
position, View convertView, ViewGroup parent) {
185.
186.
187.
LayoutInflater inflater = (LayoutInflater) context
188.
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
189.
190.
if
(convertView ==
null
) {
191.
convertView = inflater.inflate(R.layout.activity_column,
null
);
192.
}
193.
194.
195.
196.
ImageView imgPhoto = (ImageView) convertView.findViewById(R.id.ColPhoto);
197.
imgPhoto.setPadding(
5
,
5
,
5
,
5
);
198.
Bitmap bm = (Bitmap)MyArrList.get(position).get(
"PHOTO"
);
199.
if
(bm !=
null
) {
200.
imgPhoto.setImageBitmap(bm);
201.
}
else
{
202.
imgPhoto.setImageResource(android.R.drawable.sym_def_app_icon);
203.
}
204.
205.
206.
TextView txtName = (TextView) convertView.findViewById(R.id.ColName);
207.
txtName.setPadding(
10
,
0
,
0
,
0
);
208.
txtName.setText(MyArrList.get(position).get(
"NAME"
).toString());
209.
210.
211.
TextView txtPhoneNo = (TextView) convertView.findViewById(R.id.ColPhoneNo);
212.
txtPhoneNo.setText(MyArrList.get(position).get(
"PHONENO"
).toString());
213.
214.
return
convertView;
215.
216.
}
217.
}
218.
219.
220.
@Override
221.
public
boolean
onCreateOptionsMenu(Menu menu) {
222.
getMenuInflater().inflate(R.menu.activity_main, menu);
223.
return
true
;
224.
}
225.
226.
}