2013-07-03 41 views
0

我將數據添加到列表視圖中,一旦我收到消息。當偵聽器調用,然後我添加消息,但它不顯示在列表視圖上。添加數據但未在列表視圖中顯示

請幫我解決這個問題。

/** 
    * It display the chat messages. 
    */ 
    private void displayResultList() { 
     if(messageList != null && messageList.size() > 0) { 
      adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, messageList) { 
        public View getView(int position, View convertView, ViewGroup parent) { 
         if (convertView == null) { 
          convertView = LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, null); 
         } 
         ((TextView)convertView).setText(getItem(position)); 
         ((TextView)convertView).setTextColor(Color.BLACK); 
         return convertView; 
        }; 
      }; 
      setListAdapter(adapter); 
      getListView().setTextFilterEnabled(true); 
     } 
    } 

    /** 
     * This listener called when click on send button. 
     */ 
    private OnClickListener sendListener = new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      try { 
       muc.sendMessage(chatMessage.getText().toString()); 
       messageDataSource.open(); 
       messageDataSource.createRoomMessage(roomName, chatMessage.getText().toString(), userId, null); 
       messageDataSource.close(); 
       chatMessage.setText(""); 
      } catch(Exception e) { 
       Log.v("HB", "Exception at::" + e.getMessage()); 
      } 
     } 
    }; 
+1

datasource.notifyDataSourceChanged() –

+0

沒有對象'datasource'。請給予更多解釋。我添加了adapter.notifyDataSourceChanged(),但仍然沒有添加到列表視圖 –

+0

hm。你明白這個作品嗎?因爲它看起來你跳過了RTFM步驟...... –

回答

0

它解決了我的問題。我的問題是我刷新按鈕的onclick監聽器適配器列表視圖..

runOnUiThread(new Runnable() { 
    @Override 
    public void run() { 
     adapter.notifyDataSetChanged(); 
    } 
}); 
相關問題