android - XMPP好友列表显示

标签 android xmpp chat

实际上,我想在 textview 或 listview 中显示 friend 列表中的名称,请建议任何好的方法来表示它。我使用了以下代码,但它不起作用。确切地说,我正在寻找解决方案来显示具有在线离线状态的好友列表。请帮助我。

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



}

}

最佳答案

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

关于android - XMPP好友列表显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19913036/

相关文章:

xcode - 如何强制 xcode 在特定文件上使用 ARC?

javascript - react 。聊天工具包 API。 MessageList 组件错误 - 呈现来自其他房间的消息。组件生命周期和状态

ios - Pusher 聊天工具包教程未按预期工作

android - 如何为多种类型的文件(pdf、office、图像、文本)设置 android Intent 并返回路径?

android - Android 应用程序在创建动态表格布局时崩溃

android - 如何在聊天中使用 smack 通过 xmpp 发送联系人?

javascript - 使用 Strope js 连接到 Prosody XMPP 服务器 (CONNFAIL)

ios - Nativescript IOS : Textview not resizing after Input was cleared

android - 解析异常 : Connection to https://api. parse.com 被拒绝

Android 浏览器应用程序隐藏 url