Possible Duplicate:
XMPP aSmack - How can I get the current user state (offline/online/away/etc.)?
I am developing chat app on Android base on asmack lib. I display all the user on the ListView but I use an image to show online/offline user. But It return offline image only, even the user is online, here is my code
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.buddies);
Controller.getInstance().roster = Controller.getInstance().connection.getRoster();
// ArrayList<Buddy> buddies = new ArrayList<Buddy>();
Collection<RosterEntry> entries = Controller.getInstance().roster.getEntries();
Controller.getInstance().buddyList = new Buddy[entries.size()];
int i = 0;
for (RosterEntry r : entries) {
Buddy bud = new Buddy();
VCard card = new VCard();
try {
ProviderManager.getInstance().addIQProvider("vCard",
"vcard-temp", new VCardProvider());
card.load(Controller.getInstance().connection, r.getUser());
} catch (XMPPException e) {
Log.e("ChatOnAndroid", e.getMessage() + " " + r.getUser() + " "
+ e.getLocalizedMessage());
}
bud.jid = r.getUser();
bud.name = r.getName();
bud.status = Controller.getInstance().roster.getPresence(r.getUser());
Controller.getInstance().buddies.add(bud);
Controller.getInstance().buddyList[i++] = bud;
}
BuddyAdapter adapter = new BuddyAdapter(this, R.layout.buddy, Controller.getInstance().buddies);
setListAdapter(adapter);
/*
* list = (ListView) findViewById(R.id.buddiesList);
* list.setAdapter(adapter);
*/
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
startActivity(new Intent(this, Conferences.class));
}
public class BuddyAdapter extends ArrayAdapter<Buddy> {
private ArrayList<Buddy> items;
public BuddyAdapter(Context context, int textViewResourceId,
ArrayList<Buddy> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.buddy, null);
}
Buddy buddy = items.get(position);
if (buddy != null) {
TextView tt = (TextView) v.findViewById(R.id.buddyName);
ImageView iv = (ImageView) v.findViewById(R.id.buddyThumb);
//buddy.status = Controller.getInstance().roster.getPresence(buddy.jid);
if (buddy.status != null) {
buddy.img = R.drawable.status_online;
iv.setImageResource(buddy.img);
} else if (buddy.status == null) {
buddy.img = R.drawable.status_offline;
iv.setImageResource(buddy.img);
}
//iv.setImageResource(buddy.img);
if (tt != null) {
tt.setText(buddy.name);
}
}
return v;
}
}