Error: The content of the adapter has changed but ListView did not receive a notification
Hi All I have the following code:
enter code here
public class XMPPClient extends Activity {
private ArrayList<String> messages = new ArrayList<String>();
private Handler mHandler = new Handler();
private EditText mRecipient;
private EditText mSendText;
private ListView mList;
private XMPPConnection connection;
// private Chat chat;
private MultiUserChat muc;
private String to;
/**
* Called with the activity is first created.
*/
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Log.i("XMPPClient", "onCreate called");
setContentView(R.layout.chatroom);
mSendText = (EditText) this.findViewById(R.id.sendText);
Log.i("XMPPClient", "mSendText = " + mSendText);
mList = (ListView) this.findViewById(R.id.listMessages);
Log.i("XMPPClient", "mList = " + mList);
setListAdapter();
setConnection();
// Set a listener to send a chat text message
Button send = (Button) this.findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// to = mRecipient.getText().toString();
to = "nus@nus.rohit-pc";
final String text = mSendText.getText().toString();
Log.i("XMPPClient", "Sending text [" + text + "] to [" + to
+ "]");
Message msg = new Message(to, Message.Type.groupchat);
msg.setBody(OptionsActivity.uname + ":" + text);
try {
muc.sendMessage(msg);
// chat.sendMessage(text);
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setListAdapter();
}
});
OptionsActivity.pd.dismiss();
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
this.connection.disconnect();
finish();
}
return false;
}
/**
* Called by Settings dialog when a connection is establised with the XMPP
* server
*
* @param connection
*/
public void setConnection() {
this.connection = ChatApis.setupConnection();
if (connection != null) {
connection.getChatManager();
muc = new MultiUserChat(connection, "nus@nus.rohit-pc");
try {
muc.join(connection.getUser());
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
muc.addMessageListener(new PacketListener() {
@Override
public void processPacket(Packet arg0) {
// TODO Auto-generated method s开发者_如何学Pythontub
// messages.add(fromName + ":");
Message msg1 = (Message) arg0;
messages.add(msg1.getBody());
// Add the incoming message to the list view
mHandler.post(new Runnable() {
public void run() {
// if(flag){
setListAdapter();
// }
}
});
Toast.makeText(XMPPClient.this, arg0.toString(),
Toast.LENGTH_SHORT).show();
Log.i("XMPPClient", "text aaya [" + arg0.toString()
+ "] to [" + arg0.toString() + "]");
}
});
}
}
private void setListAdapter() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.multi_line_list_item, messages);
adapter.notifyDataSetChanged();
mList.setAdapter(adapter);
}
}
enter code here
can anyone point out the error? The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread.
I am really confused.
Thank rohit bhatnagar for the solution. I've resolved similarly case. After setAdapter()
for listview
, we should invoke notifyDataSetChanged()
of adapter.
solved by changing following statement :
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.multi_line_list_item, messages);
to
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.multi_line_list_item);
adapter.add(message);
精彩评论