0

This is my code to update 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");
            }
        }

This is output result after my method called

onReceive
size: 0
add new conversation to adapter
adapter.getCount(): 1

it stop at line LogManager.getLogger(getClass()).info("adapter.getCount(): " + adapter.getCount()); and lock adapter.notifyDataSetChanged(); method forever what's wrong in my code? Help me please! This is getView method

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;
    }
Pham Hong Phong
  • 145
  • 2
  • 10
  • any error after some time? – waqaslam Apr 23 '13 at 08:28
  • no! nothing changed. In right case "notify dataset changed" must be printed and my listView changed – Pham Hong Phong Apr 23 '13 at 08:40
  • If there's no ANR and no other crashes then I believe your application is working just fine. – waqaslam Apr 23 '13 at 08:45
  • if it stuck at this code adapter.notifyDataSetChanged() then loading dialog can't dismiss and my list view can't update new data – Pham Hong Phong Apr 23 '13 at 08:54
  • Can you show your getView() method of your class ListConversationAdapter ? – Gordak Apr 23 '13 at 09:19
  • 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()); – Pham Hong Phong Apr 23 '13 at 10:05
  • Well I was thinking about editing your post. We can't see the entire getView method (too many characters). – Gordak Apr 23 '13 at 10:06
  • List 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; } – Pham Hong Phong Apr 23 '13 at 10:06

1 Answers1

0

First, there are many things that seem wrong to me in your getView method...

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;

        }

Now try to debugg your app and tell us where it stops.

Gordak
  • 2,060
  • 22
  • 32
  • i've changed my code follow your suggestion and nothing changed. the listview still out of date and the dialog don't dismiss – Pham Hong Phong Apr 23 '13 at 10:44
  • Have you tried to debugg your app? I guess something is going wrong in your getView() because you don't get the "return not null view" message in your log. – Gordak Apr 23 '13 at 10:47
  • yes, i tried debug app and it stop at ThreadPoolExecutor.runWorker(ThreadPoolExecutor$Worker) line: 1069 – Pham Hong Phong Apr 23 '13 at 10:59
  • it mean method adapter.notifyDataSetChanged() don't complete execute – Pham Hong Phong Apr 23 '13 at 11:00
  • Did you put a Breakpoint at the beginning of the getView() method ? If you did so, which line exactly causes the problem ? Does it even get to that point ? If it doesn't enter the getView method at all, maybe you should give us the entire log. – Gordak Apr 23 '13 at 11:41
  • i resloved this problem by using a handler to call adapter.notifyDataSetChanged() – Pham Hong Phong Apr 25 '13 at 08:27