实现异步任务以获取朋友列表

时间:2013-12-14 16:21:51

标签: android chat

实际上我正在使用AsyncTask获取好友列表,我可以使用该列表选择我想要聊天的人。

如果您与单人聊天,它可以正常工作,但当您返回选择其他人进行聊天时,会再次调用AsyncTask。

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

1 个答案:

答案 0 :(得分:0)

尝试将此添加到您的代码中,我想当您回来时asynctask没有运行。所以在Resume()上重写。

@Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        new MyAsynch().execute();
    }