无法让所有用户进入asmack

时间:2011-12-03 04:02:35

标签: android asmack

我正在基于asmack lib的Android上开发聊天应用程序。我创建了1个用户并在好友列表中添加了3个朋友。当我使用名册来显示好友列表时。我不能让好友列表中的所有用户,只有1个用户。我的代码在这里:

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

感谢您的回答,这非常有用。我有另一个问题。如何在Android中将好友列表显示为ListView?我试着把它放在数组中,但它不起作用。

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

2 个答案:

答案 0 :(得分:3)

这是我的好友列表

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>

答案 1 :(得分:1)

您在TextView中只有一个伙伴,因为您正在更改每次迭代的文本。

尝试类似textview.setText(textview.getText()+ r.getName());或为每个好友添加一个新的TextView