在从另一个活动获取数据时苦苦挣扎

时间:2017-01-26 16:46:46

标签: java android

我可以将我的数组数据从一个活动转移到另一个活动,但不是我的字符串数据,我不知道为什么。 这是我的主要活动:

protected void onClickCityBreak(View v) {
    persons = (EditText) findViewById(R.id.txtPerson);
    days = (EditText) findViewById(R.id.txtDays);
    String p = persons.toString();
    String d = days.toString();
    String [] arrayCityBreak = getResources().getStringArray(R.array.citybreak);
    Intent myintent = new Intent(MainActivity.this, TripActivity.class);
    myintent.putExtra("PERSONS", p);
    myintent.putExtra("DAYS", d);
    myintent.putExtra("PLACES",arrayCityBreak);
    startActivity(myintent);
}

这是我发送给我的其他活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_trip);

    String person = getIntent().getStringExtra("PERSONS");
    String day = getIntent().getStringExtra("DAYS");

    TextView txtPerson = (TextView) findViewById(R.id.txtViewPersons);
    txtPerson.setText("Persons travelling: " + person);
    TextView txtDay = (TextView) findViewById(R.id.txtViewDays);
    txtDay.setText("Days of traveling: " + day);

    String[] arrayCityBreak = getIntent().getStringArrayExtra("PLACES");
    ArrayAdapter<String> adapterCityBreak = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayCityBreak);
    ListView myview = (ListView) findViewById(R.id.lstView);
    myview.setAdapter(adapterCityBreak);
}

我在应用程序中的TextView上得到了这个:

android.support.v7.widget.AppCompactEditText{13e7726VFED...CL. ........563,56....#7f0c0059 app:id/txtDays\

txtPerson也一样。 我也尝试过使用Bundle

2 个答案:

答案 0 :(得分:3)

获取EditText的内容应该以这种方式完成:

 String p = persons.getText().toString();
 String d = days.getText().toString();

答案 1 :(得分:0)

此外,如果您对putExtra有其他问题,可以执行以下操作:

MainActivity

protected void onClickCityBreak(View v) {
    persons = (EditText) findViewById(R.id.txtPerson);
    days = (EditText) findViewById(R.id.txtDays);
    String p = persons.getText().toString();
    String d = days.getText().toString();
    String [] arrayCityBreak = getResources().getStringArray(R.array.citybreak);
    Intent myintent = new Intent(MainActivity.this, TripActivity.class);
    AnotherActivityClass secondactivity = new AnotherActivityClass();
    secondactivity.persons = p;
    secondactivity.days = d;
    secondactivity.places = arrayCityBreak;    
    startActivity(myintent);
}

创建公共字符串人员和日期,也在SecondActivity中创建公共String []:

public String days;
public String persons;
public String [] places;

然后在 SecondActivity 中使用此值:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_trip);

    TextView txtPerson = (TextView) findViewById(R.id.txtViewPersons);
    txtPerson.setText("Persons travelling: " + person);
    TextView txtDay = (TextView) findViewById(R.id.txtViewDays);
    txtDay.setText("Days of traveling: " + day);

    ArrayAdapter<String> adapterCityBreak = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayCityBreak);
    ListView myview = (ListView) findViewById(R.id.lstView);
    myview.setAdapter(adapterCityBreak);
}