首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >搭载MatrixCursor的安卓listView没有更新-- futureTask

搭载MatrixCursor的安卓listView没有更新-- futureTask
EN

Stack Overflow用户
提问于 2011-11-16 20:17:03
回答 1查看 837关注 0票数 0

我正在用android编写一个简单的xmpp客户端,我在更新contactList时遇到了一个问题。我正在使用一个带有SimpleCursorAdapterMatrixCursor来做这件事。当prog更新list时,它会启动futureTask的线程。数据没问题,但是list on screen不会刷新,因为(我想)这个futureTask的线程正在等待执行?

当任何联系人更改状态时,从一个监听器调用drawContactList方法。例如,当我将call复制到main时,它工作得很好。

你知道怎么修复它吗?

代码:

代码语言:javascript
复制
static ListView contactList;
static MatrixCursor clCursor;
static SimpleCursorAdapter adapterList;

private final String[] matrixCols = new String[] { "_id", "username", "description", "icon" };
private final String[] menuCols = new String[] {"username", "description", "icon" };
private final int[] toWhatId = new int[] { R.id.clUsername, R.id.clDescription, R.id.clStatusIcon };

...
public void drawContactList(ArrayList<Contact> contacts, Context context) {

    clCursor = new MatrixCursor(matrixCols);

    startManagingCursor(clCursor);
    if(contacts != null){
        for (Contact contact : contacts) {          
            clCursor.addRow(new Object[] { contact.id, contact.name, contact.description, contact.icon });
        }
    }
    adapterList = new SimpleCursorAdapter(getApplicationContext(), R.layout.contactlist, clCursor, menuCols, toWhatId); 
    //in this line futureTask starts

    contactList.setAdapter(adapterList);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-04 02:47:33

我找到了一个解决方案。我正在使用Handler来调用这个方法。它可以完美地工作:)

代码语言:javascript
复制
private Handler contactListHandler = new Handler();

contactListHandler.post(new Runnable() {
   public void run() {
      System.out.println("Presence changed: " + presence.getFrom() + " " + presence);
      drawContactList(xmppConnection.updateContacts(presence), context);
   }
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8151528

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档