android - 将联系人的图片加载到 ListView 而不是默认图片?

标签 android contacts image contactscontract

我创建了一个包含我的联系人的 ListView ...

tab_contact_list.xml,包含 ListView :

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/tab_contact_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" />

</LinearLayout>


listview_detail_tab_contact_list.xml, ListView 的详细行:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:src="@drawable/defaultavatar" />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


        <TextView
            android:id="@+id/contact_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:text="Who am I"
            android:textAppearance="?android:attr/textAppearanceLarge" />


        <TextView
            android:id="@+id/contact_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="000000000" />

    </LinearLayout> 

</LinearLayout>


defaultavatar.png 位于文件夹drawable defaultavatar.png


而且,我有一些类(class):

public class ContactStock {

    private String name;
    private String number;

    public ContactStock(String name, String number) {
        this.name = name;
        this.number = number;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getName() {
        return this.name;
    }

    public String getNumber() {
        return this.number;
    }

}


public class ContactListAdapter extends ArrayAdapter {
    private final Activity activity;
    private final List stocks;

    public ContactListAdapter(Activity activity, List objects) {
        super(activity, R.layout.listview_detail_tab_contact_list, objects);
        this.activity = activity;
        this.stocks = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View rowView = convertView;
        ContactStockView sv = null;
        if (rowView == null) {
            // Get a new instance of the row layout view
            LayoutInflater inflater = activity.getLayoutInflater();
            rowView = inflater.inflate(
                    R.layout.listview_detail_tab_contact_list, null);

            // Hold the view objects in an object,
            // so they don't need to be re-fetched
            sv = new ContactStockView();
            sv.name = (TextView) rowView.findViewById(R.id.contact_name);

            sv.number = (TextView) rowView.findViewById(R.id.contact_number);

            // Cache the view objects in the tag,
            // so they can be re-accessed later
            rowView.setTag(sv);
        } else {
            sv = (ContactStockView) rowView.getTag();
        }
        // Transfer the stock data from the data object
        // to the view objects
        ContactStock currentStock = (ContactStock) stocks.get(position);
        sv.name.setText(currentStock.getName());
        sv.number.setText(currentStock.getNumber());

        // TODO Auto-generated method stub
        return rowView;
    }

    protected static class ContactStockView {
        protected TextView name;
        protected TextView number;
    }
}


我有类显示 ListView :

public class addlistfromcontact extends Activity {
    private ListView lst;
    private List<ContactStock> contactstock;
    private Cursor mCursor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_contact_list);
        lst = (ListView) findViewById(R.id.tab_contact_list);
        contactstock = new ArrayList<ContactStock>();

        mCursor = managedQuery(ContactsContract.Data.CONTENT_URI, null, Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'", null,
                ContactsContract.Data.DISPLAY_NAME + " ASC");
        int number = mCursor.getColumnIndex(Phone.NUMBER);      
        int name = mCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME);
        while (mCursor.moveToNext()) {
            String phName = mCursor.getString(name);
            String phNumber = mCursor.getString(number);
            contactstock.add(new ContactStock(phName, phNumber));
        }
        lst.setAdapter(new ContactListAdapter(addlistfromcontact.this,
                contactstock));
    }

}


我的结果是这样的:

Listview results


现在如何显示每个联系人的图片而不是 defaultavatar.png?

最佳答案

我相信你搞错了。

long phId=mCursor.getLong(id);

应该是 while 循环的一部分。

关于android - 将联系人的图片加载到 ListView 而不是默认图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8338038/

相关文章:

android - 如何从android中的一组Whatsapp中获取联系人?

android - 是什么导致此 PNG 渲染不佳?

java - 为什么我的 JAR 文件没有与其文件夹一起打包并读取它们?

java - 字母代码在java/android中首先执行

Android - 显示特定联系人信息

java - Java 和 Android 的线程问题

ios - SwiftUI 列表仅使用给定数组的第一项

php - 如何将图像添加到 Codeigniter 中的分页按钮

java - 从具有可变长度/字段的数组中获取 ID 到类?

android - AutoTransition 在 Motion 布局中不起作用