六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 8|回复: 0

在联系人中放入图片

[复制链接]

升级  82%

292

主题

292

主题

292

主题

进士

Rank: 4

积分
910
 楼主| 发表于 2013-2-4 19:17:08 | 显示全部楼层 |阅读模式

 
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <ImageView        android:src="@drawable/icon"        android:layout_width="wrap_content"        android:layout_height="wrap_content"    />    <LinearLayout        android:orientation="vertical"        android:layout_width="fill_parent"        android:layout_height="fill_parent">        <LinearLayout            android:orientation="horizontal"            android:layout_width="wrap_content"            android:layout_height="wrap_content">            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Name: "            />            <TextView android:id="@+id/contact_name"                android:layout_width="wrap_content"                android:layout_height="wrap_content"            />        </LinearLayout>        <LinearLayout            android:orientation="horizontal"            android:layout_width="wrap_content"            android:layout_height="wrap_content">            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Phone: "            />            <TextView android:id="@+id/phone_number"                android:layout_width="wrap_content"                android:layout_height="wrap_content"            />        </LinearLayout>    </LinearLayout></LinearLayout><uses-permission android:name="android.permission.READ_CONTACTS" /><uses-permission android:name="android.permission.CALL_PHONE" />public class ListContacts extends ListActivity {
 
    private SimpleCursorAdapter myAdapter;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
        startManagingCursor(cursor);
 
        // start mappings
        String[] columns = new String[] {People.NAME, People.NUMBER};
        int[] names = new int[] {R.id.contact_name, R.id.phone_number};
 
        myAdapter = new SimpleCursorAdapter(this, R.layout.main, cursor, columns, names);
        setListAdapter(myAdapter);
    }
 
    @Override
    protected void onListItemClick(ListView listView, View view, int position, long id) {
        super.onListItemClick(listView, view, position, id);
 
        Intent intent = new Intent(Intent.ACTION_CALL);
        Cursor cursor = (Cursor) myAdapter.getItem(position);
        long phoneId = cursor.getLong(cursor.getColumnIndex(People.PRIMARY_PHONE_ID));
        intent.setData(ContentUris.withAppendedId(Phones.CONTENT_URI, phoneId));
 
        startActivity(intent);
    }
}
 
这里一定要注意 myAdapter.getItem(position);取出来的对象是cursor

public class MySimpleCursorAdapter extends SimpleCursorAdapter {
    private Cursor _cursor;
    private Context _context;
 
    public MySimpleCursorAdapter
(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        _cursor = c;
        _context = context;
    }
 
   
/**
     * {@inheritDoc}
     */

    @Override
    public
void bindView(View view, Context context, Cursor cursor) {
        ImageView imageView = (ImageView) view.findViewById(R.id.contact_image);
 
       
int id = _cursor.getColumnIndex(People._ID);
        Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, _cursor.getLong(id));
 
        Bitmap bitmap
= People.loadContactPhoto(_context, uri, R.drawable.icon, null);
 
        imageView.
setImageBitmap(bitmap);
 
        super.
bindView(view, context, cursor);
    }
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表