0

这是我更新 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;
    }
4

1 回答 1

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;

        }

现在尝试调试您的应用程序并告诉我们它在哪里停止。

于 2013-04-23T10:35:47.817 回答