将联系人的图片加载到列表视图中而不是默认的?
我创建了一个包含我的联系人的列表视图...
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,列表视图的详细信息行:
<?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?
I created a listview containing my contacts...
tab_contact_list.xml, contains the 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, details rows of 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 is in folder drawable
And, I have some classes:
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;
}
}
I have class display the 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));
}
}
And my result looks like this:
Now how do I display each contact's picture instead of defaultavatar.png?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信你有一个错误。应该
是 while 循环的一部分。
I believe you have a mistake. The
should be part of the while loop.
尝试使用您的模型来保存图片..
确保您在 ArrayAdapter 中实现 getPicture() 方法,您只在那里使用您的名称和数字对象,在您的行的 xml 中您已经有图像视图,只需将其添加到ArrayAdapter ...那么如何获取位图?这对我有用...
StackOverflow 成员“wrongmissle”在此发布了此内容。 如何加载联系人照片?
谢谢错误的导弹。
由于您已经可以获得联系人的姓名和号码,只需提取 id,将其插入到带有 ContentResolver 的方法中,该方法会返回一个可以放置在您的列表中的位图!希望有帮助。
Try using your Model to hold the picture..
make sure you implement the getPicture() method in your ArrayAdapter, you only use your name and number object there, in your xml for your row you have the imageview there already, just add it to the ArrayAdapter ... so how do you get the bitmap?? this worked for me...
StackOverflow member "wrongmissle" posted this here. How do I load a contact Photo?
thanks wrongmissle.
Since you can already get the name and number of a contact just pull the id, plug it into that method with a ContentResolver, that returns a bitmap that can be placed on your list! hope that helped.
上面的答案可能会给你带来错误...这是一个 100% 工作的代码
,其中 ImageView 是朋友。
The above answer might give you error... Here is a 100% working code
Where ImageView is friend.
首先,您需要向
ContactStock
类添加一个位图,这将保存联系人照片。
将适配器类的末尾更改为以下代码:
向主活动 (
AddlistFromContact
) 添加以下函数:现在您需要做的就是添加到
onCreate()
> 当然,在定义了光标之后,使用这两行函数:并按以下方式调用 contactStock 的构造函数:
希望我有所帮助
first you need to add a bitmap to the
ContactStock
class,this will save the contact photo.
change the end of the adapter class to the following code:
to the main activity (
AddlistFromContact
) add the following function:now all you need to do is add to your
onCreate()
function this two lines, after you have defined the cursor of course:and call the constractur of contactStock in the following way:
hope I've helped