gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:03:30 24 4
gpt4 key购买 nike

我创建了一个包含我的联系人的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com