Asmack未在Process Message中更新列表视图

时间:2013-12-02 13:32:02

标签: android smack asmack

我正在尝试更新列表视图,同时处理来自asmack的消息无法正常工作。

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chatpage);
msgList = (ListView) findViewById(R.id.listView);
        myList = new ArrayList<String>();
        myarrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, myList);

        msgList.setAdapter(myarrayAdapter);

        enterText = (EditText) findViewById(R.id.txt_inputText);
        Button btnSend = (Button) findViewById(R.id.btn_Send);
        ChatManager chatmanager = client.getUChatManager().getChatManager();
        newChat = chatmanager.createChat("test@localhost",
                new MessageListener() {

                    @Override
                    public void processMessage(Chat chat, Message message) {
                        myList.add("asd");
                        myarrayAdapter.notifyDataSetChanged();
                    }
                });
        btnSend.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                try {
                    newChat.sendMessage(enterText.getText().toString());
                    myList.add(enterText.getText().toString());
                    myarrayAdapter.notifyDataSetChanged();
                } catch (XMPPException e) {
                    System.out.println("Error Delivering block");
                }

            }
        });
}

2 个答案:

答案 0 :(得分:0)

 You should use list.invalidateViews(); after setadapter

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chatpage);
    msgList=(ListView)findViewById(R.id.listView);
    myList = new ArrayList<String>();
    myarrayAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, myList);
    msgList.setAdapter(myarrayAdapter);
    msgList.invalidateViews();

    enterText = (EditText) findViewById(R.id.txt_inputText);
    Button btnSend = (Button) findViewById(R.id.btn_Send);
    ChatManager chatmanager = client.getUChatManager().getChatManager();
    newChat = chatmanager.createChat("test@localhost",
            new MessageListener() {

                @Override
                public void processMessage(Chat chat, Message message) {
                    myList.add("asd");
                    myarrayAdapter.notifyDataSetChanged();
                }
            });
    btnSend.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            try {
                newChat.sendMessage(enterText.getText().toString());
                myList.add(enterText.getText().toString());
                myarrayAdapter.notifyDataSetChanged();
            } catch (XMPPException e) {
                System.out.println("Error Delivering block");
            }

        }
    });
}

答案 1 :(得分:0)

You need to set the adpater after notify changed 

 btnSend.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        try {
            newChat.sendMessage(enterText.getText().toString());
            myList.add(enterText.getText().toString());
            myarrayAdapter.notifyDataSetChanged();
              msgList.setAdapter(myarrayAdapter);
              msgList.invalidateViews();
        } catch (XMPPException e) {
            System.out.println("Error Delivering block");
        }

    }
});