Question

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, just 1 user only. My code here:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);         
     setContentView(R.layout.tab_set);


     TextView textview = new TextView(this);    
     setContentView(textview);

     Roster roster = Controller.getInstance().connection.getRoster();
     Collection<RosterEntry> entries = roster.getEntries();

     for(RosterEntry r:entries)
     {
         textview.setText(r.getName());
     }

Thank for your answer, it is very useful. I have another question. How to display buddy list as ListView in android ? I try to put it in the array but it not work.

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);         
     setContentView(R.layout.tab_set);

     String buddy;

     Roster roster = Controller.getInstance().connection.getRoster();
     Collection<RosterEntry> entries = roster.getEntries();

     for(RosterEntry r:entries)
     {

         buddy = textview.getText() + r.getName();
         Controller.getInstance().values = new String[]{buddy};
     }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, Controller.getInstance().values);
    setListAdapter(adapter);
Was it helpful?

Solution

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

OTHER TIPS

This is my buddies list

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;
}

Layout 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>

And layout 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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top