简体   繁体   中英

Having trouble getting value from Intent in Android

Working on a project and ran into an issue with getting values from intent. I've looked at the android documentation and read the StackOverflow post, and it seems I'm doing it correctly, but still getting an error.

Intent intent = new Intent(this, DetailedTerm.class);
        // pass data to the detail view
        Term term = Objects.requireNonNull(termViewModel.allTerms.getValue()).get(position);
        intent.putExtra("id", term.getId());
        intent.putExtra("title", term.getTitle());
        intent.putExtra("start", term.getStartDate());
        intent.putExtra("end", term.getEndDate());

        Log.d("TAG", "Term values -----------------------------------\n"
                + term.getId() + " " 
                + term.getTitle() + " " 
                + term.getStartDate() + " " 
                + term.getEndDate());

        startActivity(intent);

Log confirms I am getting the values here ^^

Here is the receiving activity, I've tried it using getStringExtra() for all the intents and specifying the type of intent. The first gives me a null pointer, the second throws a type cast error.

// get values from term card and set text
        id.setText(getIntent().getStringExtra("id"));
        title.setText(getIntent().getStringExtra("title"));
        start.setText(getIntent().getStringExtra("start"));
        end.setText(getIntent().getStringExtra("end"));
        Log.d("TAG", "Term values in Detail Term\n"
                + getIntent().getIntExtra("id") + " "
                + getIntent().getStringExtra("title") + " "
                + getIntent().getFloatExtra("start") + " "
                + getIntent().getFloatExtra("end"));

I was able to solve my problem, so rather than deleting this question, I am going to post lessons learned.

There were several things going wrong for me.

  1. putExtra() allows you to pass almost any value type along with a String key-value pair, but on the other side you have to specify the type your pulling out.

For instance.

title.setText(getIntent().getStringExtra("title"));

Here I have to specify I am pulling a string with the key name in quotes.

id = getIntent().getIntExtra("id", 0);

Here I have to specify getIntExtra that I am pulling an Integer, with the key name in quotes.

  1. This brings to my next problem, when passing numeric values, you must pull them out with a key name but ALSO a default value. See above next to id there is 0.

  2. Lastly when storing a Date in a Room database you need a type converter which handles conversion from Date type to Long type. That I knew, what i didn't know its this is applied atomatlic only when inserting to and from the database not everywhere else so i have to Explistliy cast the value to and from when sending along with an Intent, Like so.

intent.putExtra("start", DateConverter.ToTimestamp(term.getStartDate()));

and then pulling out the date

startDate = DateConverter.fromTimestamp(getIntent().getLongExtra("start", 0));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM