2

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

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



}

}

4

1 回答 1

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();
    }       
    }       
    }   
于 2013-12-05T07:04:37.947 回答