0
嗨测试一个示例gcm应用程序,但每次我发送一条消息,我不能在listview中添加另一个值。发生的是以前的消息正在被新消息取代。正在更新listview with arrayadapter
这里是我的代码:
对不起编码差。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_memo);
getActionBar().setDisplayHomeAsUpEnabled(true);
list1 = (ListView)findViewById(R.id.listView1);
registerReceiver(HandleMemoReceiver, new IntentFilter(DISPLAY_MESSAGE_ACTION));
}
private final BroadcastReceiver HandleMemoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
//memo.append(newMessage + "\n");
ArrayList<String> nm = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Memo.this, android.R.layout.simple_list_item_1,nm);
list1.setAdapter(adapter);
adapter.add(newMessage);
adapter.notifyDataSetChanged();
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.memo, menu);
return true;
}
public void onBackPressed() {
finish();//go back to the previous Activity
overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
}