Android的带有MatrixCursor的listView不会更新 - futureTask

时间:2011-11-16 12:17:03

标签: android listview futuretask matrixcursor

我在android中编写一个简单的xmpp客户端,我遇到更新contactList的问题。我使用MatrixCursorSimpleCursorAdapter来做。当prog更新列表时,它启动futureTask的线程。数据还可以,但是屏幕上的列表没有刷新,因为(我认为)这个futureTask的线程正在等待执行?

当任何联系人更改状态时,从一个侦听器调用

drawContactList方法。例如,当我将呼叫复制到main时,它可以很好地工作。

知道怎么解决吗?

代码:

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

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。我使用Handler调用此方法。它完美地运作:)

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