Home > Back-end >  Send String from Edittext on 1st Activity to the Edittext on the 2nd Activity
Send String from Edittext on 1st Activity to the Edittext on the 2nd Activity

Time:12-25

I have some string that i write into the Edittext on my first activity and im trying to send it to the Edittext on the other activity by pressing Button, but my code does not work, nothing happen and i dont know what else can i do here maybe somebody could help me out

My onClick method at 1st activity:

public void calendarOR(View v, AdapterView<?> parent, int position, long id) {
        String data = cala.getText().toString();
        Intent intent = new Intent(getApplicationContext(), calendar.class);
        intent.putExtra("id",data);
        startActivity(intent);

My second activity code:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    userId = extras.getLong("id");
}
if (userId > 0) {
    userCursor = db.rawQuery("select * from "   dbelper.TABLE   " where "   dbelper.COLUMN_ID   "=?", new String[]{String.valueOf(userId)});
    userCursor.moveToFirst();
    date.setText(userCursor.getString(1));
    userCursor.close();
}

CodePudding user response:

You are putting a String data type and trying to get a long. Change the second activity to get the "id" as a string.

userId = extras.getString("id");

then

if (userId != null) {
    userCursor = db.rawQuery("select * from "   dbelper.TABLE   " where "   dbelper.COLUMN_ID   "=?", new String[]{userId});
    userCursor.moveToFirst();
    date.setText(userCursor.getString(1));
    userCursor.close();
}

You may also want to print log messages if userId is null or the data is not in the database to help with debugging.

Unrelated, but you may want to change your intent to not use the application context, like this, to launch the calendar activity.

Intent intent = new Intent(this, calendar.class);

CodePudding user response:

do this in second activity code:

String data =getIntent().getStringExtra("id");

  • Related