这是我的好友列表
public class BuddiesList extends Activity {
ListView list;
Roster roster;
static Buddy[] buddyList;
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.buddies);
roster = AsmackTestingActivity.xmpp.getRoster();
ArrayList<Buddy> buddies = new ArrayList<Buddy>();
Collection<RosterEntry> entries = roster.getEntries();
buddyList = new Buddy[entries.size()];
ProviderManager.getInstance().addIQProvider("vCard", "vcard-temp", new VCardProvider());
Buddy bud = null;
VCard card = null;
int i = 0;
for (RosterEntry entry:entries) {
bud = new Buddy();
card = new VCard();
try {
card.load(AsmackTestingActivity.xmpp, entry.getUser());
} catch (Exception e) {
e.printStackTrace();
}
bud.jid = entry.getUser();
bud.name = card.getField("FN");
if (bud.name == null)
bud.name = bud.jid;
buddies.add(bud);
byte[] img = card.getAvatar();
if (img != null) {
int len = img.length;
bud.img = BitmapFactory.decodeByteArray(img, 0, len);
}
buddyList[i++] = bud;
}
final BuddyAdapter adapter = new BuddyAdapter(this, buddies);
list = (ListView) findViewById(R.id.buddiesList);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(BuddiesList.this, AsmackChat.class);
intent.putExtra("username", adapter.getItem(position).jid);
startActivity(intent);
}
});
}
class BuddyAdapter extends ArrayAdapter<Buddy> {
public BuddyAdapter(Context context, ArrayList<Buddy> items) {
super(context, R.id.buddyName, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parentView) {
Buddy bud = getItem(position);
ViewHolder holder = null;
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.buddy, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.buddyName);
holder.thumb = (ImageView) convertView.findViewById(R.id.buddyThumb);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(bud.name);
holder.thumb.setImageBitmap(bud.img);
return convertView;
}
}
class Buddy {
String jid;
String name;
int status;
Bitmap img;
}
class ViewHolder {
ImageView thumb;
TextView name;
}
布局buddies.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/buddiesList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
并布局buddy.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<ImageView
android:id="@+id/buddyThumb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="5dp" />
<TextView
android:id="@+id/buddyName"
android:layout_toRightOf="@id/buddyThumb"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF"/>
</RelativeLayout>