我有一个聊天基础应用程序,当我收到用户的消息时,我的CustomListViewAdapter没有更新。我有alrady使用了adapter.notifyDataSetChanged();但它在我的情况下不起作用。这是我的代码
public class Messages extends Activity implements
OnItemClickListener{
private RowItem item;
ListView listView;
public static List<RowItem> rowItems;
private CustomListViewAdapter adapter;
//private ArrayList<String> messages = new ArrayList();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub\
View view= inflater.inflate(R.layout.messages,null);
return view;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.messages);
rowItems = new ArrayList<RowItem>();
listView = (ListView) findViewById(R.id.list1);
item = new RowItem(R.drawable.messages, "messages");
rowItems.add(item);
adapter = new CustomListViewAdapter(this,
R.layout.list_item, rowItems);
final EditText msg1 = (EditText)findViewById(R.id.msg);
Button send = (Button) this.findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(msg1.getText().toString().equals(""))
{
Toast.makeText(getApplicationContext(), "type message", Toast.LENGTH_LONG).show();
}else
{
String to = "asghar@web.vlivetech.com";
String text = msg1.getText().toString();
msg1.setText("");
Log.i("XMPPClient", "Sending text [" + text + "] to [" + to + "]");
Message msg = new Message(to, Message.Type.chat);
msg.setBody(text);
Network.connection.sendPacket(msg);
//messages.add(Network.connection.getUser() + ":");
//messages.add(text);
item = new RowItem(R.drawable.connection, text);
rowItems.add(item);
adapter = new CustomListViewAdapter(getBaseContext(),
R.layout.list_item, rowItems);
listView.setAdapter(adapter);
//setListAdapter();
}
}
});
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
Network.connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message.getFrom());
Log.i("XMPPClient", "Got text [" + message.getBody() + "] from [" + fromName + "]");
//recieve.setText(message.getBody());
/*messages.add(fromName + ":");
messages.add(message.getBody());*/
// Add the incoming message to the list view
item = new RowItem(R.drawable.billing, message.getBody());
adapter = new CustomListViewAdapter(getBaseContext(),
R.layout.list_item, rowItems);
rowItems.add(item);
//adapter.notifyDataSetChanged();
//adapter.notifyDataSetChanged();
//listView.setAdapter(adapter);
}
}
}, filter);
PacketFilter filter1 = new MessageTypeFilter(Message.Type.headline);
Network.connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message.getFrom());
Log.i("XMPPClient", "Got text wth xml [" + message.toXML() + "] from [" + fromName + "]");
//recieve.setText(message.getBody());
/*messages.add(fromName + ":");
messages.add(message.getBody());*/
// Add the incoming message to the list view
item = new RowItem(R.drawable.billing, message.getBody());
adapter = new CustomListViewAdapter(getBaseContext(),
R.layout.list_item, rowItems);
rowItems.add(item);
//adapter.notifyDataSetInvalidated();
//adapter.notifyDataSetChanged();
//listView.setAdapter(adapter);
}
}
}, filter1);
//listView.setAdapter(adapter);
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.messages, menu);
return true;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
}
}
答案 0 :(得分:0)
尝试此代码如果有效,我会说明原因:
public class Messages extends Activity implements
OnItemClickListener{
private RowItem item;
ListView listView;
public static List<RowItem> rowItems = new ArrayList<RowItem>();
private CustomListViewAdapter adapter;
//private ArrayList<String> messages = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.messages);
listView = (ListView) findViewById(R.id.list1);
item = new RowItem(R.drawable.messages, "messages");
rowItems.add(item);
adapter = new CustomListViewAdapter(this,
R.layout.list_item, rowItems);
final EditText msg1 = (EditText)findViewById(R.id.msg);
Button send = (Button) this.findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(msg1.getText().toString().equals(""))
{
Toast.makeText(getApplicationContext(), "type message", Toast.LENGTH_LONG).show();
}else
{
String to = "asghar@web.vlivetech.com";
String text = msg1.getText().toString();
msg1.setText("");
Log.i("XMPPClient", "Sending text [" + text + "] to [" + to + "]");
Message msg = new Message(to, Message.Type.chat);
msg.setBody(text);
Network.connection.sendPacket(msg);
//messages.add(Network.connection.getUser() + ":");
//messages.add(text);
item = new RowItem(R.drawable.connection, text);
rowItems.add(item);
adapter.notifyDataSetChanged();
}
}
});
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
Network.connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message.getFrom());
Log.i("XMPPClient", "Got text [" + message.getBody() + "] from [" + fromName + "]");
//recieve.setText(message.getBody());
/*messages.add(fromName + ":");
messages.add(message.getBody());*/
// Add the incoming message to the list view
item = new RowItem(R.drawable.billing, message.getBody());
rowItems.add(item);
adapter.notifyDataSetChanged();
}
}
}, filter);
PacketFilter filter1 = new MessageTypeFilter(Message.Type.headline);
Network.connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message.getFrom());
Log.i("XMPPClient", "Got text wth xml [" + message.toXML() + "] from [" + fromName + "]");
//recieve.setText(message.getBody());
/*messages.add(fromName + ":");
messages.add(message.getBody());*/
// Add the incoming message to the list view
item = new RowItem(R.drawable.billing, message.getBody());
rowItems.add(item);
adapter.notifyDataSetInvalidated();
}
}
}, filter1);
}