2014-09-02 57 views
0

我有一個問題。我需要動態更改選項菜單項圖標,但我所嘗試的一切似乎都不起作用。我正在使用Android的兼容性庫,我打電話給supportInvalidateOptionsMenu。我已經覆蓋了onPrepareOptionsMenu,因爲根據文檔,這是正確的方式。但每次我調用supportInvalidateOptionsMenu時,onCreateOptionsMenu總是在onPrepareOptionsMenu之前調用。這是賴特嗎?這裏是我的代碼:ActionBar optionsMenu調用後不會更新supportInvalidateOptionsMenu

@覆蓋 公共布爾onPrepareOptionsMenu(菜單菜單){

MenuItem actionNote = menu.findItem(R.id.action_note); 

    db.open(); 
    int notesCount = db.getNotificationsCount(CurrentConnection.username); 
    db.close(); 

    switch (notesCount) { 
    case 0: 
     actionNote.setIcon(R.drawable.notify_icon); 
     break; 
    case 1: 
     actionNote.setIcon(R.drawable.notify_icon1); 
     actionNote.setVisible(false); 
     break; 
    case 2: 
     actionNote.setIcon(R.drawable.notify_icon2); 
     break; 
    case 3: 
     actionNote.setIcon(R.drawable.notify_icon3); 
     break; 
    default: 
     actionNote.setIcon(R.drawable.notify_icon3m); 
     break; 
    } 
    return super.onPrepareOptionsMenu(menu); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.home, menu); 
    MenuItem action_note = menu.findItem(R.id.action_note); 

    db.open(); 
    int notesCount = db.getNotificationsCount(CurrentConnection.username); 
    db.close(); 

    switch (notesCount) { 
    case 0: 
     action_note.setIcon(R.drawable.notify_icon); 
     break; 
    case 1: 
     action_note.setIcon(R.drawable.notify_icon1); 
     break; 
    case 2: 
     action_note.setIcon(R.drawable.notify_icon2); 
     break; 
    case 3: 
     action_note.setIcon(R.drawable.notify_icon3); 
     break; 
    default: 
     action_note.setIcon(R.drawable.notify_icon3m); 
     break; 
    } 
    return true; 
} 

我呼籲supportInvalidateOptionsMenu:

PacketFilter filter = new MessageTypeFilter(Message.Type.chat); 
    CurrentConnection.connection.addPacketListener(new PacketListener() { 
     @Override 
     public void processPacket(Packet packet) { 
      Message message = (Message) packet; 
      if (message.getBody() != null) { 

       String[] fields = message.getBody().split(":"); 

       String subject = fields[1]; 
       int type = Integer.parseInt(fields[2]); 
       String sender_username = fields[3]; 
       String sender_name = fields[4], receiver_username = fields[5], receiver_name = fields[6]; 

       db.open(); 

       db.insertNotification(sender_username, sender_name, 
         receiver_username, receiver_name, subject, type); 

       db.close(); 
       supportInvalidateOptionsMenu(); 
      } 
     } 
    }, filter); 

這是剛剛從asmack libray監聽器(一個XMPP客戶端庫)。請我需要幫助。

回答

0

其實答案很簡單。 PacketListener在另一個線程中運行,所以用戶界面無法從中更新。只需要使用處理程序在處理程序中調用supportInvalidateOptionsMenu。

messagesHandler.post(new Runnable() { 

        @Override 
        public void run() { 
         PublicChatFragment publicChatFragment = (PublicChatFragment) getSupportFragmentManager() 
           .findFragmentByTag(PublicChatFragment.TAG); 
         publicChatFragment.setAdapter(); 
        } 
       }); 

messagesHandler是在UI線程中創建的,因此它解決了這個問題。

相關問題