我创建了一个包含我的联系人的列表视图......
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
而且,我有一些课程:
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));
}
}
我的结果如下:
现在我如何显示每个联系人的图片而不是defaultavatar.png?
答案 0 :(得分:0)
尝试使用您的模型来保存图片..
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将其插入到该方法中,该方法返回一个可以放在列表中的位图!希望有所帮助。
答案 1 :(得分:0)
上述答案可能会给您错误......以下是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是朋友。
答案 2 :(得分:0)
首先你需要向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的constractur:
contactstock.add(new ContactStock(phName, phNumber,getPhoto(phId)));
希望我帮助
答案 3 :(得分:0)
我相信你错了。在
long phId=mCursor.getLong(id);
应该是while循环的一部分。