intent.putExtra()用于多个文本框

时间:2014-09-25 16:18:46

标签: android eclipse

我正在尝试修改Android应用以满足我的需求。 原始应用程序的第1页显示注释列表,第2页显示详细注释。 我想要实现的不是在详细的笔记页面中只有一个文本框,我希望它有几个文本框,并且也坚持它。

我认为这是怎么回事(但当然失败了)。 这是第2页(详细页面)活动的发射器:

private void saveAndFinish()
{
EditText et = (EditText) findViewById(R.id.eventTitle);
String eventTitle = et.getText().toString();

EditText et2 = (EditText) findViewById(R.id.eventDate);
String eventDate = et2.getText().toString();

EditText et3 = (EditText) findViewById(R.id.eventVenue);
String eventVenue = et3.getText().toString();

EditText et4 = (EditText) findViewById(R.id.eventLocation);
String eventLocation = et4.getText().toString();

EditText et5 = (EditText) findViewById(R.id.eventNote);
String eventNote = et5.getText().toString();

EditText et6 = (EditText) findViewById(R.id.eventAttendees);
String eventAttendees = et6.getText().toString();


Intent intent = new Intent();
// pass these to the main activity will ya?
intent.putExtra("key", data.getKey());
intent.putExtra("title", eventTitle); // eventTitle is the edited text!
intent.putExtra("date", eventDate); 
intent.putExtra("venue", eventVenue); 
intent.putExtra("location", eventLocation); 
intent.putExtra("note", eventNote); 
intent.putExtra("attendees", eventAttendees); 

setResult(RESULT_OK, intent);
// work done, go back to calling activity
finish();
}

这是第1页(主页)活动的接收者:

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if (requestCode==DETAIL_ACTIVITY_REQ && resultCode==RESULT_OK) 
    {
        DataItem event = new DataItem();
        event.setKey(data.getStringExtra("key"));
        event.setTitle(data.getStringExtra("title"));
        event.setDate(data.getStringExtra("date"));
        event.setVenue(data.getStringExtra("venue"));
        event.setLocation(data.getStringExtra("location"));
        event.setNote(data.getStringExtra("note"));
        event.setAttendees(data.getStringExtra("attendees"));

    datasource.update(event);
    refreshDisplay();
    }
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) 
{
    // which list view item was selected?
    DataItem data = eventsList.get(position);

    //now, to navigate. which class should i go to next?
    Intent intent = new Intent(this, DetailedActivity.class); 
    // and also, pass data to the next activity will ya?
    intent.putExtra("key", data.getKey());
    intent.putExtra("title", data.getTitle());
    intent.putExtra("date", data.getDate());
    intent.putExtra("venue", data.getVenue());
    intent.putExtra("location", data.getLocation());
    intent.putExtra("note", data.getNote());
    intent.putExtra("attendees", data.getAttendees());
    // go go go!
    startActivityForResult(intent, DETAIL_ACTIVITY_REQ);
}

显然只有"标题"被保存,其他一切都没有。帮助

2 个答案:

答案 0 :(得分:0)

在saveAndFinish方法中修改你的代码

Intent intent = new Intent(thisActivityname.this,secondActivityname.class);

startActivity(意向);

上面的代码是放入数据并发送到第二个活动。

现在获得第二项活动的上述数据

Intent intent = getIntent();

String title = intent.getStringExtra(" title");

String date = intent.getStringExtra(" date");

String venue = intent.getStringExtra("场地");

String location = intent.getStringExtra(" location");

String note = intent.getStringExtra(" note");

String attendees = intent.getStringExtra("与会者");

答案 1 :(得分:0)

您必须使用getIntent()从Intent获取数据:

data = getIntent(); 

在ActivityResult上使用:

data = getIntent();