在我的主要活动中我使用putextra()使用意图使用数据来设置数据 方法
private void editHandler()
{
//send the details to another form
if (itemID > 0)
{
Bundle values = new Bundle();
singleItem = (TODOListItem) adapter.getItem(itemID);
Intent intent = new Intent(this,AddorEdit.class);
values.putString("text",singleItem.getText());
values.putString("date", singleItem.getDate());
values.putString("time", singleItem.getDate());
values.putInt("id", singleItem.getItemID());
values.putInt("alarm", singleItem.getAlarm());
intent.putExtra("bundle", values);
startActivity(intent);
}
在下一个活动中我收到了这个意图
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addor_edit);
todoNote = (EditText)findViewById(R.id.txt_todoNote);
todoDate = (EditText) findViewById(R.id.txt_dateTODO);
todoTime = (EditText) findViewById(R.id.txt_timeTODO);
todoalarm =(ToggleButton) findViewById(R.id.toggle_alarm);
alarmEnable = (ImageView) findViewById(R.id.img_alarmEnable);
canceltodo = (Button) findViewById(R.id.btn_cancelTODO);
maketodo = (Button) findViewById(R.id.btn_makeTODO);
//Receiving intent
Bundle bundle = getIntent().getBundleExtra("bundle");
getValuesForEdit(bundle);
todoalarm.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
if(todoalarm.getText().equals("ON"))
{
alarmEnable.setImageResource(R.drawable.dark_alarm);
alarm = 1;
}
else
{
alarmEnable.setImageResource(0);
alarm = 0;
}
}
});
maketodo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
addnewTODO();
}
});
}
private void getValuesForEdit(Bundle bundle)
{
// if the Edit Button is pressed get all values from listView
ID = bundle.getInt("id");
todoNote.setText(bundle.getString("text"));
todoDate.setText(bundle.getString("date"));
todoTime.setText(bundle.getString("time"));
alarm = bundle.getInt("alarm");
if (alarm == 1)
{
todoalarm.setText("ON");
alarmEnable.setImageResource(R.drawable.dark_alarm);
}
}
并且应用程序崩溃
我的意图做错了吗?这是在两者之间传递数据的正确方法 活动?建议和建议是必要的......谢谢!
答案 0 :(得分:1)
Intent intent = new Intent(this,AddorEdit.class);
intent.putExtra("text",singleItem.getText());
intent.putExtra("date", singleItem.getDate());
intent.putExtra("time", singleItem.getDate());
intent.putExtra("id", singleItem.getItemID());
intent.putExtra("alarm", singleItem.getAlarm());
startActivity(intent);
//获取此数据时:
ID = getIntent().getIntExtra("id");
todoNote.setText(getIntent().getStringExtra("text"));
todoDate.setText(getIntent().getStringExtra("date"));
todoTime.setText(getIntent().getStringExtra("time"));
alarm = getIntent().getIntExtra;
答案 1 :(得分:0)
试试这个
Bundle values = new Bundle();
singleItem = (TODOListItem) adapter.getItem(itemID);
Intent intent = new Intent(this,AddorEdit.class);
values.putString("text",singleItem.getText());
values.putString("date", singleItem.getDate());
values.putString("time", singleItem.getDate());
values.putInt("id", singleItem.getItemID());
values.putInt("alarm", singleItem.getAlarm());
intent.putExtras(values);
startActivity(intent);
和
Bundle bndl = this.getIntent().getExtras()