package myapplication.nomasp.com.addressbook;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView lvPhoneInfo;
private PhoneAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GetNumber.getNumber(this);
lvPhoneInfo = (ListView)findViewById(R.id.lvPhoneInfo);
adapter = new PhoneAdapter(GetNumber.lists,this);
lvPhoneInfo.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
package myapplication.nomasp.com.addressbook;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/** * Created by nomasp on 2015/09/22. */
public class GetNumber {
public static List<PhoneInfo> lists = new ArrayList<PhoneInfo>();
public static String getNumber(Context context){
Cursor cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
String phoneNumber;
String phoneName;
ContentResolver resolver = context.getContentResolver();
while (cursor.moveToNext()) {
phoneNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
phoneName = cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));
PhoneInfo phoneInfo = new PhoneInfo(phoneNumber,phoneName);
lists.add(phoneInfo);
}
return null;
}
}
package myapplication.nomasp.com.addressbook;
import android.graphics.Bitmap;
/** * Created by nomasp on 2015/09/22. */
public class PhoneInfo {
private String name;
private String number;
public PhoneInfo(String number, String name) {
this.number = number;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
PhoneAdapter.java
package myapplication.nomasp.com.addressbook;
import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
import java.util.concurrent.TimeoutException;
/** * Created by nomasp on 2015/09/22. */
public class PhoneAdapter extends BaseAdapter {
private List<PhoneInfo> lists;
private Context context;
private LinearLayout layout;
public PhoneAdapter(List<PhoneInfo> lists, Context context) {
this.lists = lists;
this.context = context;
}
@Override
public int getCount() {
return lists.size();
}
@Override
public Object getItem(int position) {
return lists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView = LayoutInflater.from(context).inflate(R.layout.cell,null);
holder = new ViewHolder();
holder.tvName = (TextView)convertView.findViewById(R.id.tvName);
holder.tvNumber = (TextView)convertView.findViewById(R.id.tvNumber);
holder.ivPicture = (ImageView)convertView.findViewById(R.id.ivPicture);
holder.tvName.setText(lists.get(position).getName());
holder.tvNumber.setText(lists.get(position).getNumber());
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
holder.tvName.setText(lists.get(position).getName());
holder.tvNumber.setText(lists.get(position).getNumber());
}
return convertView;
}
private static class ViewHolder{
TextView tvName;
TextView tvNumber;
ImageView ivPicture;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
<ListView android:id="@+id/lvPhoneInfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentStart="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal">
<ImageView android:id="@+id/ivPicture" android:layout_width="45dp" android:layout_height="45dp"/>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="horizontal">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">
<TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:textSize="15sp"/>
<TextView android:id="@+id/tvNumber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:textSize="11sp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="myapplication.nomasp.com.addressbook" android:versionCode="1" android:versionName="1.0" >
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="14" android:maxSdkVersion="22" android:targetSdkVersion="21"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
</manifest>