views:

69

answers:

2

Hello!

I am very new to android, so I am experimenting with the tutorial code. In particular, the notepad.

If you look here: http://developer.android.com/resources/tutorials/notepad/notepad-ex2.html, at Step 5 they are putting values so that it can be used by NoteEdit.

I would like to pass a reference of NotesDbAdapter into NoteEdit so that I can actually do some entirely different things.

It does not seem to invoke a constructor though. I have tried adding a paramiter to NoteEdit onCreate, but it does not seem to like this? :-(

I most look forward to hearing your answers, experts! :-D

+3  A: 

Put/Get extras are not passed via constructors, you have to explicitly read from the intent, probably in the onCreate method

First Activity

Intent myIntent = new Intent();
myIntent.putExtra("key", "value");
startActivity(myIntent); 

New Activity

code in onCreate

Intent myIntent = getIntent(); 
myIntent.getExtra("key");

If you want to pass custom classes, your class must implement serializable and you then read by:

Bundle e = getIntent().getExtras();
Object obj=(Object) e.getSerializable("key");
Pentium10
My "custom" class should implement serializable?
Gibbon
Would it be better to to the database work in First Activity and pass that in with the intent.It would need to have two details. My values from database is like this:"animal","cat""animal","dog""food","potato"
Gibbon
Create a reduced class that stores fields you want to pass, kinda enum type class. This class will implement serializable. Then you put the data into that class on whatever activity or model level you want. You can that pass it to another activity with the intent, and you will read the data as you want. Keep it simple if you need to pass 5 fields, you create a class having 5 members to store the data. Or even simpler you can use the Extras to avoid this step. I don't know how complicate is your result.
Pentium10
they values are in pairs:Kind (i.e. Animal or food)Instance (Cat, potato)But, cats are never food ;-) lol
Gibbon
A: 

I agree with Mr. Castiblanco. Do not try to do this.

Your Adapter holds a reference to the main Activity, the one with the ListView that the Adapter is attached to. If you were to get your Adapter over to the new Activity, then your new Activity will hold a reference to your old Activity. This will prevent Android from being able to destroy your old Activity to free up memory for other applications, since your new Activity will hold a reference to it, preventing garbage collection.

If you need to share a data model between activities, hold that data model outside of those activities (e.g., database, Service, Application). Your Adapter is not your data model.

CommonsWare
So, I should redeclare the database class and connection in my Edit class?
Gibbon
OR, as I said in another comment.Some kind of array map that is passed in the extras?
Gibbon
"I should redeclare the database class and connection in my Edit class?" That is one approach. "Some kind of array map that is passed in the extras?" As I wrote, if you need to share a data model between activities, hold that data model outside of those activities (e.g., database, Service, Application). Passing your entire data model between activities via extras is not an approach I would recommend.
CommonsWare