在android中调用notifyDataSetChanged()方法时卡住了

时间:2013-04-23 08:18:51

标签: android adapter notifydatasetchanged

这是我更新ListView的代码

public void onReceive(Conversation newConversation, Message message) {
                LogManager.getLogger(getClass()).info("onReceive");
                Conversation conversation = null;
                ListConversationAdapter adapter = (ListConversationAdapter)listViewConversation.getAdapter();
                int size = adapter.getCount();
                LogManager.getLogger(getClass()).info("size: " + size);
                if(size > 0)
                {
                    for(int i=0;i<size;i++)
                    {
                        if(adapter.getItemId(i) == newConversation.getId())
                        {
                            conversation = (Conversation)adapter.getItem(i);
                            break;
                        }
                    }
                }

                if(conversation == null)
                {
                    adapter.addItem(newConversation);
                    LogManager.getLogger(getClass()).info("add new conversation to adapter");
                }
                LogManager.getLogger(getClass()).info("adapter.getCount(): " + adapter.getCount());
                adapter.notifyDataSetChanged();
                LogManager.getLogger(getClass()).info("notify dataset changed");
            }
        }

这是我的方法名为

后的输出结果
onReceive
size: 0
add new conversation to adapter
adapter.getCount(): 1

它停在LogManager.getLogger(getClass())行.info(“adapter.getCount():”+ adapter.getCount());并锁定adapter.notifyDataSetChanged();方法永远在我的代码中有什么问题? 请帮帮我! 这是getView方法

public View getView(int position, View convertView, ViewGroup parent) {
        Conversation conversation = conversations.get(position);
        if(conversation != null)
        {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            LinearLayout parentView = (LinearLayout) inflater.inflate(R.layout.conversation_item, null);

            TextView textViewName = (TextView)parentView.findViewById(R.id.textViewName);
            TextView textViewLastMessage = (TextView)parentView.findViewById(R.id.textViewLastMessage);

            textViewName.setText(conversation.getName());

            List<Message> messages = MessageManager.getProvider().getMessages(conversation.getId(), 0, 0);
            if(messages.size() > 0)
            {
                Message message = messages.get(messages.size() - 1);
                textViewLastMessage.setText(Emoticon.getSmiledText(message.getBody()));
            }

            LogManager.getLogger(getClass()).info("return not null view");
            return parentView;
        }
        LogManager.getLogger(getClass()).info("return null view");
        return null;
    }

1 个答案:

答案 0 :(得分:0)

首先,在你的getView方法中,有很多东西对我来说是错误的......

public View getView(int position, View convertView, ViewGroup parent) {
        Conversation conversation = conversations.get(position);
        if(convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = (LinearLayout) inflater.inflate(R.layout.conversation_item, parent);
        }
            TextView textViewName = (TextView)convertView.findViewById(R.id.textViewName);
            TextView textViewLastMessage = (TextView)convertView.findViewById(R.id.textViewLastMessage);

       if(conversation=null{
            textViewName.setText(conversation.getName());

            List<Message> messages = MessageManager.getProvider().getMessages(conversation.getId(), 0, 0);
            if(messages.size() > 0)
            {
                Message message = messages.get(messages.size() - 1);
                textViewLastMessage.setText(Emoticon.getSmiledText(message.getBody()));
            }
      }
      LogManager.getLogger(getClass()).info("return not null view");
      return convertView;

        }

现在尝试调试您的应用并告诉我们它停止的位置。