0

我正在用 android 编写一个简单的 xmpp 客户端,但 update 有问题contactList。我使用MatrixCursorwithSimpleCursorAdapter来做到这一点。当 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);
}
4

1 回答 1

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);
   }
});
于 2011-12-03T18:47:32.953 回答