XMPP好友列表显示

时间:2013-11-11 18:14:17

标签: android xmpp chat

我想在textview或listview中显示来自friendlist的名字,请提出任何好方法来表示它。我使用以下代码,但它不工作。我正在寻找解决方案来显示在线离线状态的frienlist。请帮助我。

    public void connect() {

    final ProgressDialog dialog = ProgressDialog.show(this,
            "Connecting...", "Please wait...", false);

    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            // Create a connection
            ConnectionConfiguration connConfig = new ConnectionConfiguration(
                    HOST, PORT, SERVICE);
            XMPPConnection connection = new XMPPConnection(connConfig);
            try {
                connection.connect();
                Log.i("XMPPChatDemoActivity",
                        "Connected to " + connection.getHost());
            } catch (XMPPException ex) {
                Log.e("XMPPChatDemoActivity", "Failed to connect to "
                        + connection.getHost());
                Log.e("XMPPChatDemoActivity", ex.toString());

            }

            try {
                // SASLAuthentication.supportSASLMechanism("PLAIN", 0);
                connection.login(USERNAME, PASSWORD);
                Log.i("XMPPChatDemoActivity",
                        "Logged in as " + connection.getUser());

                // Set the status to available
                Presence presence = new Presence(Presence.Type.available);
                connection.sendPacket(presence);
                Roster roster = connection.getRoster();
                Collection<RosterEntry> entries = roster.getEntries();
                sb = new StringBuffer();
                for (RosterEntry entry : entries) {
                    Presence entryPresence = roster.getPresence(entry
                            .getUser());
                    String number1 = entry.getUser();

                    String name1 = entry.getName();
                    String status = entryPresence.getStatus();
                    String TYpe;
                    Presence.Type type = entryPresence.getType();
                    if (type == Presence.Type.available){
                        TYpe="available";
                        }else{
                        TYpe="unavailable";
                        }
                        sb.append( "\nName:---"+number1 );
                        Log.e(number1, "names");
                        sb.append("\n----------------------------------");
                }

                name.setText(sb);

            }

                catch (XMPPException ex) {
                    Log.e("XMPPChatDemoActivity", "Failed to log in as "
                            + USERNAME);
                    Log.e("XMPPChatDemoActivity", ex.toString());

                } 

                dialog.dismiss();

        }


    });
        t.start();
        dialog.show();



}

}

1 个答案:

答案 0 :(得分:2)

public class Done extends Activity{
StringBuilder sb;
List<RowItems> rowItems;
CustomListViewAdapter adapter;
private ListView lst;
String TYpe;
Bitmap img ;

  List<String> myVal = new ArrayList<String>() ;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.lists);
    lst=(ListView)findViewById(R.id.listView1);
    new MyAsynch().execute();
}

    private void populate() {

    rowItems=new ArrayList<RowItems>();

    Roster roster = connection.getRoster();
    Collection<RosterEntry> entries = roster.getEntries();
     ProviderManager.getInstance().addIQProvider("vCard", "vcard-temp",
             new VCardProvider());
     VCard card = null;
    for (RosterEntry entry : entries) {
         card = new VCard();
         try {
             card.load(connection, entry.getUser());
         } catch (Exception e) {
             e.printStackTrace();
         }
        String user=entry.getUser();

        String name=entry.getName();

        Presence entryPresence = roster.getPresence(entry
                .getUser());
        Presence.Type type = entryPresence.getType();
        if (type == Presence.Type.available){


        byte[] imgs = card.getAvatar();
        if (imgs != null) {
            int len = imgs.length;
            img = BitmapFactory.decodeByteArray(imgs, 0, len);
        }
        RowItems p=new RowItems(user,name,img);
        rowItems.add(p);
    }



}   

lst.setOnItemClickListener(new OnItemClickListener(){       
@Override
public void onItemClick(AdapterView<?> parent, View arg1, int arg2, long arg3) {
        TextView textViewOne = (TextView)arg1.findViewById(R.id.desc);
        TextView textViewTwo = (TextView)arg1.findViewById(R.id.title);
     String item = textViewOne.getText().toString();
     String items = textViewTwo.getText().toString();
    Toast.makeText(getApplicationContext(), item +"\n"+ items,
              Toast.LENGTH_LONG).show(); 
    Intent intent = new Intent(Done.this, Chat.class);
    intent.putExtra("contentOne", item);
    intent.putExtra("contentTwo", items);
    startActivity(intent);
    }

});
}
    private class MyAsynch extends AsyncTask<Void, Void, Void>{
     ProgressDialog dialog;


    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        dialog = ProgressDialog.show(Done.this,
                "Loading...", "Getting friendlist", true);
    }
    @Override
    protected Void doInBackground(Void... arg0) {

        populate();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        super.onPostExecute(result);
        adapter= new CustomListViewAdapter(getApplicationContext(), R.layout.activity_listitem, rowItems);
        lst.setAdapter(adapter);
        dialog.dismiss();
    }       
    }       
    }