Unable To Get All User In Asmack
I am developing chat app on Android base on asmack lib. I create 1 user and add 3 friend in buddy list. When I use roster to display buddy list. I can't get all user in buddy list,
Solution 1:
This is my buddies list
publicclassBuddiesListextendsActivity {
ListView list;
Roster roster;
static Buddy[] buddyList;
publicvoidonCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.buddies);
roster = AsmackTestingActivity.xmpp.getRoster();
ArrayList<Buddy> buddies = newArrayList<Buddy>();
Collection<RosterEntry> entries = roster.getEntries();
buddyList = newBuddy[entries.size()];
ProviderManager.getInstance().addIQProvider("vCard", "vcard-temp", newVCardProvider());
Buddybud=null;
VCardcard=null;
inti=0;
for (RosterEntry entry:entries) {
bud = newBuddy();
card = newVCard();
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) {
intlen= img.length;
bud.img = BitmapFactory.decodeByteArray(img, 0, len);
}
buddyList[i++] = bud;
}
finalBuddyAdapteradapter=newBuddyAdapter(this, buddies);
list = (ListView) findViewById(R.id.buddiesList);
list.setAdapter(adapter);
list.setOnItemClickListener(newOnItemClickListener() {
publicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {
Intentintent=newIntent(BuddiesList.this, AsmackChat.class);
intent.putExtra("username", adapter.getItem(position).jid);
startActivity(intent);
}
});
}
classBuddyAdapterextendsArrayAdapter<Buddy> {
publicBuddyAdapter(Context context, ArrayList<Buddy> items) {
super(context, R.id.buddyName, items);
}
@Overridepublic View getView(int position, View convertView, ViewGroup parentView) {
Buddybud= getItem(position);
ViewHolderholder=null;
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.buddy, null);
holder = newViewHolder();
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;
}
}
classBuddy {
String jid;
String name;
int status;
Bitmap img;
}
classViewHolder {
ImageView thumb;
TextView name;
}
Layout buddies.xml:
<?xml version="1.0" encoding="utf-8"?><ListViewxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/buddiesList"android:layout_width="fill_parent"android:layout_height="fill_parent" ></ListView>
And layout buddy.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:padding="5dp"><ImageViewandroid:id="@+id/buddyThumb"android:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingRight="5dp" /><TextViewandroid: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>
Solution 2:
You are just having one buddy in your TextView because you are changing the text for each iteration.
Try something like textview.setText(textview.getText() + r.getName()); or add a new TextView for each buddy
Post a Comment for "Unable To Get All User In Asmack"