【问题标题】:Load a contact's picture into a listview instead of the default?将联系人的图片加载到列表视图中,而不是默认?
【发布时间】:2012-01-10 09:40:02
【问题描述】:

我创建了一个包含我的联系人的列表视图...

tab_contact_list.xml,包含列表视图:

    <?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


而且,我有一些课程:

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;
    }
}


我有类显示列表视图:

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));
    }

}


我的结果是这样的:


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

【问题讨论】:

  • 请回答您的问题 Thuong 我也遇到了同样的问题

标签: android contacts image contactscontract


【解决方案1】:

我相信你有一个错误。

long phId=mCursor.getLong(id);

应该是while循环的一部分。

【讨论】:

    【解决方案2】:

    尝试使用您的模型来握住图片..

    public class ContactStock {
    
        private Bitmap picture;
    
        // your current code goes here       
    
        public void setPicture(Bitmap picture) {
            this.picture = picture;
        }
    
        public Bitmap getPicture() {
            return picture;
        }
    }
    

    确保您在 ArrayAdapter 中实现 getPicture() 方法,您只在那里使用您的姓名和数字对象,在您的行的 xml 中,您已经有 imageview,只需将其添加到 ArrayAdapter ...那么如何你有位图吗??这对我有用...

    public Bitmap loadContactPhoto(ContentResolver cr, long id) { 
        Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id); 
        InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri); 
        if (input == null) { 
    return null; 
        } 
        return BitmapFactory.decodeStream(input); 
    }
    

    StackOverflow 成员“wrongmissle”在此处发布了此内容。 How do I load a contact Photo? 谢谢错误的导弹。 由于您已经可以获取联系人的姓名和号码,只需拉出 id,使用 ContentResolver 将其插入该方法,该方法会返回一个可以放在您的列表中的位图!希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      上面的答案可能会给你错误...这是一个 100% 工作的代码

      Uri uri = Uri.parse(url);
                                   Bitmap bitmap = null;
                                  try {
                                      bitmap = MediaStore.Images.Media.getBitmap(contexts.getContentResolver(), uri);
                                  } catch (FileNotFoundException e) {
                                      // TODO Auto-generated catch block
                                      bitmap = null;
                                      e.printStackTrace();
                                  } catch (IOException e) {
                                      // TODO Auto-generated catch block
                                      e.printStackTrace();
                                      bitmap = null;
                                  }
      
      
                                  if(bitmap != null)
                                      friend.setImageURI(uri);
                                  else
                                      friend.setImageResource(R.drawable.avatar_photo); 
      

      ImageView 是朋友。

      【讨论】:

        【解决方案4】:

        首先你需要给ContactStock类添加一个位图, 这将保存联系人照片。

        将适配器类的结尾改成如下代码:

                ContactStock currentStock = (ContactStock) stocks.get(position);
                sv.name.setText(currentStock.getName());
                sv.number.setText(currentStock.getNumber());
                sv.image.setImageBitmap(currentStock.getPicture());
        
        
                // TODO Auto-generated method stub
                return rowView;
            }
        
            protected static class ContactStockView {
                protected TextView name;
                protected TextView number;
                protected ImageView image;
            }
        

        向主活动(AddlistFromContact)添加以下函数:

        public Bitmap getPhoto(long userId ) {
                Uri photoUri = null;
                ContentResolver cr = this.getContentResolver();
                photoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, userId);
                Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.contacts);
                if (photoUri != null) {
                    InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(
                            cr, photoUri);
                    if (input != null) {
                        return BitmapFactory.decodeStream(input);
                    }
                } else {
        
                    return defaultPhoto;
                }
        
                return defaultPhoto;
            }
        

        现在你需要做的就是在你的onCreate()函数中添加这两行,当然在你定义了光标之后:

        
        
            int id=mCursor.getColumnIndex(Phone.CONTACT_ID);
            long phId=mCursor.getLong(id);
        
        

        并通过以下方式调用contactStock的构造器:

        contactstock.add(new ContactStock(phName, phNumber,getPhoto(phId)));
        

        希望我有所帮助

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-22
          • 2012-08-26
          • 1970-01-01
          • 1970-01-01
          • 2014-07-12
          • 2019-06-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多