tryAndroid
tryAndroid

Reputation: 95

Struggling with getting data from another activity

I can transfer my array-data from an activity to the other, but not my string-data, and I don't know why. This is my mainActivity:

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);
}

This is my other activity I am sending to:

@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);
}

I get this on my TextView in the application:

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

The same for txtPerson. I have also tried using Bundle

Upvotes: 0

Views: 42

Answers (2)

user5364144
user5364144

Reputation:

Also if you have another problems with putExtra, you can do something like this:

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);
}

Create public string persons and days, also create public String[] in SecondActivity:

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

And then in SecondActivity use this values:

@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);
}

Upvotes: 0

Ahmed Abidi
Ahmed Abidi

Reputation: 1077

Getting the content of an EditText should be done in this way :

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

Upvotes: 3

Related Questions