Android Bundle corrupted when passing between activities? - android

I'm trying to pass an ArrayList of Parcelable objects plus a string value between two activities. This is the code to add the data to the intent and pass it through:
Intent intent = new Intent(this, DisplayLotListActivity.class);
Bundle dataBundle = new Bundle();
dataBundle.putParcelableArrayList(DisplayLotListActivity.EXTRA_LOT_ARRAY, lotList);
dataBundle.putString(EXTRA_LOT_NUMBER, lotNumber);
intent.putExtra(DisplayLotListActivity.EXTRA_DATA, dataBundle);
startActivity(intent);
This is the code that I'm using to get the data out of the intent on the target activity:
Intent intent = getIntent();
Bundle dataBundle = intent.getBundleExtra(EXTRA_DATA);
lotList = dataBundle.getParcelableArrayList(EXTRA_LOT_ARRAY);
lotNumber = dataBundle.getString(LotInquiryActivity.EXTRA_LOT_NUMBER);
When I check the debugger the data structures look correct before the activity is called but when I get into the target activity the data structure has been corrupted. Specifically the ArrayList as 3 elements and it is still 3 elements in size but the second element is null. There is then an additional extra in the bundle which contains the missing element object with a null key. I have images of the debugger before and after but can't put them in the post because of anti-spam rules.
Before: http://i.stack.imgur.com/vDipq.png
After: http://i.stack.imgur.com/JqbF7.png
Is there something I'm missing? This issue occurs whether I use a Bundle or add the ArrayList directly to the intent. This is being run on a Samsung Tab 2 running 4.0.3. This also occurs with a 4.0 emulator.
[Edit]
This is the Parcelable object being used (I've just left the getter and setter methods off the bottom)
public class Lot implements Parcelable{
private String lotn;
private String dsc1;
private String dsc2;
private String litm;
private long itm;
private String locn;
private String mcu;
private String uom1;
private String uom2;
private BigDecimal pqav;
private BigDecimal pqoh;
private BigDecimal sqoh;
private long vend;
private String rlot;
private String ldsc;
private String lots;
private String lot1;
private String lot2;
private String lot3;
private String lotsdsc;
private XMLGregorianCalendar mmej;
private XMLGregorianCalendar ohdj;
public Lot(){
}
public Lot(Parcel source){
lotn = source.readString();
dsc1 = source.readString();
dsc2 = source.readString();
litm = source.readString();
locn = source.readString();
mcu = source.readString();
uom1 = source.readString();
uom2 = source.readString();
itm = source.readLong();
pqav = new BigDecimal(source.readString());
pqoh = new BigDecimal(source.readString());
sqoh = new BigDecimal(source.readString());
vend = source.readLong();
rlot = source.readString();
ldsc = source.readString();
lots = source.readString();
lot1 = source.readString();
lot2 = source.readString();
lot3 = source.readString();
lotsdsc = source.readString();
try{
mmej = DatatypeFactory.newInstance().newXMLGregorianCalendar(source.readString());
}catch (Exception exc){
mmej = null;
}
try{
ohdj = DatatypeFactory.newInstance().newXMLGregorianCalendar(source.readString());
}catch (Exception exc){
ohdj = null;
}
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(lotn);
dest.writeString(dsc1);
dest.writeString(dsc2);
dest.writeString(litm);
dest.writeString(locn);
dest.writeString(mcu);
dest.writeString(uom1);
dest.writeString(uom2);
dest.writeLong(itm);
if(pqav != null){
dest.writeString(pqav.toPlainString());
} else {
dest.writeString("0");
}
if(pqoh != null){
dest.writeString(pqoh.toPlainString());
} else {
dest.writeString("0");
}
if(sqoh != null){
dest.writeString(sqoh.toPlainString());
} else {
dest.writeString("0");
}
dest.writeLong(vend);
dest.writeString(rlot);
dest.writeString(ldsc);
dest.writeString(lots);
dest.writeString(lot1);
dest.writeString(lot2);
dest.writeString(lot3);
dest.writeString(lotsdsc);
if(mmej != null){
dest.writeString(mmej.toXMLFormat());
} else {
dest.writeString("");
}
if(ohdj != null){
dest.writeString(ohdj.toXMLFormat());
} else {
dest.writeString("");
}
}
/**
*
*/
public static final Parcelable.Creator<Lot> CREATOR
= new Parcelable.Creator<Lot>() {
public Lot createFromParcel(Parcel in) {
return new Lot(in);
}
public Lot[] newArray(int size) {
return new Lot[size];
}
};

OK, for anyone that comes back to this question there's two problems that I found which I assume combined to cause this behavior. The issues all related to the Parcelable object I was using so thanks to Todd for at least pointing me in this direction.
Firstly, I had a simple error where I had missed a readString() in my constructor of the Parcelable object. So basically I was writing out n elements and reading in n - 1 elements. The second issue is that Android does not implement the javax.xml.datatype library which means that XMLGregorianCalendar is not available. As I didn't need the features of this class on the client side (there's a Java Web Application that it talks to which does use it) I just converted over to a simple java.util.Date object instead.

Related

Parcelable how to use the 'KEY' value when receiving all the information?

I'm making an Android application and I would like to send an ArrayList to the next Activity, but I don't know how to do this with Parcelable.
For example: I got an int named as ID and a String named as Names. I use JSON for getting all informations from PHP and MySQL.
In the for-loop I add all those Names and ID to a class. But then I don't know how to write all these Names and ID into a bundle.putParcelableArrayList(KEY, ArrayList...);, because I don't know how to do this with the KEY value. Do I need to give the KEY value numbers or is there automatically one KEY?
class alleDeelnemers implements Parcelable {
int id;
String name;
/* GETTER AND SETTER */
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(volgnummer);
}
public alleDeelnemers(Parcel in) {
volgnummer = in.readString();
id = in.readInt();
}
// Creator
public static final Parcelable.Creator CREATOR
= new Parcelable.Creator() {
public alleDeelnemers createFromParcel(Parcel in) {
return new alleDeelnemers(in);
}
public alleDeelnemers[] newArray(int size) {
return new alleDeelnemers[size];
}
};
}
Receiving data:
ArrayList<alleDeelnemers> deelnemers = new ArrayList<>();
for(int i=0; i < jArrayDeelnemer.length(); i++)
{
JSONObject json_data = jArrayDeelnemer.getJSONObject(i);
//class alleDeelnemers
deelnemer = new alleDeelnemers();
//ID
int ID = json_data.getInt("ID");
deelnemer.setId(ID);
//alle deelnemers
String name = json_data.getString("name ");
deelnemer.setName(name );
//toevoegen in de class
deelnemers.add(deelnemer);
}
error this line: deelnemer = new alleDeelnemers(); need to build a constructor.
The key is any name you would use to reference your ArrayList. Further in the started activity, you will use this key to retrieve the ArrayList. For example, in the initial activity, do
bundle.putParcelableArrayList("MY_ARRAY_LIST", deelnemers);
and then in the started activity, to retrieve it later
Bundle b = getIntent().getExtras();
ArrayList<alleDeelnemers> list = b.getParcelableArrayList("MY_ARRAY_LIST");
Note that the KEY used here is "MY_ARRAY_LIST". It is a better practice to use xml string constants or an interface for storing constant KEYS.
1.First create your alleDeelnemers class as Parcelable using plug-in
2 Sender Activity
Intent intent = new Intent(this, YourActivity.class);
intent.putExtra("mData", myArrayList);
3 Receiver Activity
myArrayList = getIntent().getParcelableExtra("mData")

Parcelable array not passing between activities

So, I'm having an issue with passing a Parcelable array between two activities. For some reason, the app blows up before it even loads the activity I'm calling.
Here is the object I am trying to pass.
import android.os.Parcel;
import android.os.Parcelable;
public class Inventory implements Parcelable{
public String inventory_id = "" ;
public String vin = "" ;
public String stock = "" ;
public String new_used = "" ;
public String certified = "" ;
public String mileage = "" ;
public Inventory() {}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(inventory_id);
dest.writeString(vin);
dest.writeString(stock);
dest.writeString(new_used);
dest.writeString(certified);
dest.writeString(mileage);
}
public Inventory(Parcel source){
inventory_id = source.readString();
vin = source.readString();
stock = source.readString();
new_used = source.readString();
certified = source.readString();
mileage = source.readString();
}
public static final Parcelable.Creator<Inventory> CREATOR = new Parcelable.Creator<Inventory>() {
public Inventory createFromParcel(Parcel source) {
return new Inventory(source);
}
public Inventory[] newArray(int size) {
return new Inventory[size];
}
};
}
And here is the line where I pass it
Intent intent = new Intent(this, Activity2.class);
intent.putParcelableArrayListExtra("invList", invList); //This is a list of inventory objects.
startActivity(intent);
This code never loads Activity2. I can create a debug point in the onCreate method in Activity 2 and it is never called. If I comment out intent.putParcelableArrayListExtra("invList", invList);, Activity2 will load, but of course blows up when I get to the line that needs the invList, which leads me to believe that something is happening with the Parcelable objects between activities. I get nothing in my LogCat. It just reloads Activity1.
Any help here would be appreciated.

Android save array of custom objects

I have a question about saving an arraylist of custom objects. I have a class called notitie:
public class Notitie implements Serializable{
private String titel = "";
private String type = "";
private String datum = "";
public void setTitel (String titel){
this.titel = titel;
}
public String getTitel(){
return titel;
}
public void setType (String type){
this.type = type;
}
public String getType(){
return type;
}
public void setDatum (String datum){
this.datum = datum;
}
public String getDatum(){
return datum;
}
}
I create some objects of Notitie and add them to my arraylist called notities
ArrayList<Notitie> notities = new ArrayList<Notitie>();
Notitie notitie1 = new Notitie();
notitie1.setTitel("Meting");
notitie1.setType("Watermeting");
notitie1.setDatum("22-09-12");
notities.add(notitie1);
Notitie notitie2 = new Notitie();
notitie1.setTitel("Meting2");
notitie1.setType("Watermeting2");
notitie1.setDatum("23-09-12");
notities.add(notitie2);
Notitie notitie3 = new Notitie();
notitie1.setTitel("Meting3");
notitie1.setType("Watermeting3");
notitie1.setDatum("24-09-12");
notities.add(notitie3);
Now I want to save the filled Arraylist on the device's storage so it can be accessed anytime. I used to save data as a String or some Integers with sharedpreferences but I can't save this Arraylist with that.
Does anybody have a solution?
Thanks in advance!
You do have a few options:
Use serialization, XML or JSON, and store your data in a File. Refer to this solution if you wanna implement serialization.
Store you data using SQLite. Have a look at this tutorial to get started.
EDIT : You might want to read this as well!

Android - How to create a basic class and use it?

I have defined a class that contains properties of a specific answer object
The class look like this and is defined inside the class that is trying to use it
protected class Answer {
String QuestionId = "";
String AnswerValue = "";
String Correct = "";
public String getQuestionId() {
return QuestionId;
}
public void setQuestionId(String arg) {
QuestionId = arg;
}
public String getAnswerValue() {
return AnswerValue;
}
public void setAnswerValue(String arg) {
AnswerValue = arg;
}
public String getCorrect() {
return Correct;
}
public void setCorrect(String arg) {
Correct = arg;
}
}
Not sure if the above is OK
When I try to use the class I get null pointer errors
I'm using it like this
ArrayList<Answer> answerList = new ArrayList<Answer>();
for(int a=0;a<answers.getLength(); a++){
Element eAnswer = (Element) answers.item(a);
Answer anAnswer = new Answer;
NodeList answer_nodes = eAnswer.getChildNodes();
for (int ian=0; ian<answer_nodes.getLength(); ian++){
Node ans_attr = answer_nodes.item(ian);
String tag_name = ans_attr.getNodeName();
if(tag_name.equalsIgnoreCase("answer")){
anAnswer.setAnswerValue(ans_attr.getTextContent());
}
}
answerList.add(anAnswer);
}
Answer anAnswer = new Answer; gives a compilation error
All I'm trying to do is to create a list of answers which have a name value pair for a number of properties
Any guidance on this greatly appreciated - Especially if there is a better way
Answer anAnswer = new Answer();

Android: Passing extra from one activity to another activity

I have a JSON file which is populated to an activity (Main.java).
This Activity shows 3 random images from the URL on my JSON entries.
What I wanna do is: I have 13 different entries on the my JSON, whenever I click the shown random picture it goes to another activity (ProjectDetail.java) containing the picture,title,and description depends on the item I click based on its entry on the JSON.
What do I have in is by using extra by I dont know exactly how to perform that since I'm using JSON. What should I add into my top_listener method on my Main class and what should I add into my ProjectDetail class?
Thank you.
Main.java
public class Main extends Activity {
/** Called when the activity is first created. */
ArrayList<Project> prjcts=null;
private ImageThreadLoader imageLoader = new ImageThreadLoader();
private final static String TAG = "MediaItemAdapter";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
prjcts = new ArrayList<Project>();
WebService webService = new WebService("http://liebenwald.spendino.net/admanager/dev/android/projects.json");
Map<String, String> params = new HashMap<String, String>();
params.put("var", "");
String response = webService.webGet("", params);
try
{
Type collectionType = new TypeToken<ArrayList<Project>>(){}.getType();
List<Project> lst= new Gson().fromJson(response, collectionType);
for(Project l : lst)
{
prjcts.add(l);
ConstantData.projectsList.add(l);
}
}
catch(Exception e)
{
Log.d("Error: ", e.getMessage());
}
final Button project = (Button) findViewById(R.id.btn_projectslist);
final Button infos = (Button) findViewById(R.id.btn_infos);
final Button contact = (Button) findViewById(R.id.btn_contact);
project.setOnClickListener(project_listener);
infos.setOnClickListener(infos_listener);
contact.setOnClickListener(contact_listener);
ImageView image1;
ImageView image2;
ImageView image3;
try {
image1 = (ImageView)findViewById(R.id.top1);
image2 = (ImageView)findViewById(R.id.top2);
image3 = (ImageView)findViewById(R.id.top3);
} catch( ClassCastException e ) {
Log.e(TAG, "Your layout must provide an image and a text view with ID's icon and text.", e);
throw e;
}
Bitmap cachedImage1 = null;
Bitmap cachedImage2 = null;
Bitmap cachedImage3 = null;
//randomize the index of image entry
int max = prjcts.size();
List<Integer> indices = new ArrayList<Integer>(max);
for(int c = 0; c < max; ++c)
{
indices.add(c);
}
int arrIndex = (int)((double)indices.size() * Math.random());
int randomIndex1 = indices.get(arrIndex);
indices.remove(arrIndex);
int randomIndex2 = indices.get(arrIndex);
indices.remove(arrIndex);
int randomIndex3 = indices.get(arrIndex);
indices.remove(arrIndex);
setImage(cachedImage1, image1, prjcts.get(randomIndex1));
setImage(cachedImage2, image2, prjcts.get(randomIndex2));
setImage(cachedImage3, image3, prjcts.get(randomIndex3));
image1.setOnClickListener(top_listener);
image2.setOnClickListener(top_listener);
image3.setOnClickListener(top_listener);
}
public void setImage(Bitmap cachedImage, final ImageView image, Project pro)
{
//Bitmap cachedImage1 = null;
try {
cachedImage = imageLoader.loadImage(pro.smallImageUrl, new ImageLoadedListener()
{
public void imageLoaded(Bitmap imageBitmap)
{
image.setImageBitmap(imageBitmap);
//notifyDataSetChanged();
}
});
} catch (MalformedURLException e) {
Log.e(TAG, "Bad remote image URL: " + pro.smallImageUrl, e);
}
if( cachedImage != null ) {
image.setImageBitmap(cachedImage);
}
}
private OnClickListener top_listener = new OnClickListener() {
public void onClick(View v) {
Intent top = new Intent(Main.this, InfosActivity.class);
startActivity(top);
}
};
ProjectDetail.java
public class ProjectDetail extends Activity implements OnClickListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.project);
Button weitersagen = (Button) findViewById(R.id.btn_weitersagen);
weitersagen.setOnClickListener(this);
Button sms = (Button) findViewById(R.id.btn_sms_spenden);
sms.setOnClickListener(this);
int position = getIntent().getExtras().getInt("spendino.de.ProjectDetail.position");
Project project = ConstantData.projectsList.get(position);
try {
ImageView projectImage = (ImageView)findViewById(R.id.project_image);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(project.bigImageUrl).getContent());
projectImage.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
TextView project_title = (TextView)findViewById(R.id.txt_project_title);
project_title.setText(project.project_title);
TextView organization_title = (TextView)findViewById(R.id.txt_organization_title);
organization_title.setText(Html.fromHtml("von " +project.organization_title));
TextView project_description = (TextView)findViewById(R.id.txt_project_description);
project_description.setText(Html.fromHtml(project.project_description));
}
I also have this ConstantData.java, the index which holds my JSON properties:
public class ConstantData{
public static String project_title = "project title";
public static String organization_title = "organization title";
public static String keyword = "keyword";
public static String short_code = "short code";
public static String project_description = "description";
public static String smallImageUrl = "smallImageUrl";
public static String bigImageUrl = "bigImageUrl";
public static String price= "price";
public static String country= "country";
public static ArrayList<Project> projectsList = new ArrayList<Project>();
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeString(project_title);
out.writeString(organization_title);
out.writeString(keyword);
out.writeString(short_code);
out.writeString(project_description);
out.writeString(smallImageUrl);
out.writeString(bigImageUrl);
out.writeString(price);
out.writeString(country);
}
public static final Parcelable.Creator<ConstantData> CREATOR
= new Parcelable.Creator<ConstantData>() {
public ConstantData createFromParcel(Parcel in) {
return new ConstantData(in);
}
public ConstantData[] newArray(int size) {
return new ConstantData[size];
}
};
private ConstantData(Parcel in) {
project_title = in.readString();
organization_title = in.readString();
keyword = in.readString();
short_code = in.readString();
project_description = in.readString();
smallImageUrl = in.readString();
bigImageUrl = in.readString();
price = in.readString();
country = in.readString();
}
}
You could make the class ConstantData serializable by extending from Parcelable and implementing a couple of methods (see the documentation). Then you could pass a constantData instance as an extra by doing
intent.putExtra("jsonData", constantDataInstance);
and retrieving it from the other activity (in it's onCreate() method) with
getIntent().getExtras().getParcelable("jsonData");
Otherwise you could just past as extra every field independently, but it would be a mess. This way is not only more easy to read and everything, but "well designed".
To pass information from one activity to another when you start the new one you do the following:
Intent top = new Intent(Main.this, InfosActivity.class);
Bundle b = new Bundle();
b.putString("key1", "value2");
b.putString("key2", "value2");
b.putString("key3", "value3");
top.putExtras(b);
startActivity(top);
Then in the newly started activity, in the onCreate() put the following:
Bundle b = getIntent().getExtras();
b.get("key1");
b.get("key2");
b.get("key3");
This will get the values from the previous activity by using the key you provided.
For more complex objects you should extend Parcelable (probably what you'll need) and then use:
b.putParcelable("Key4", yourParcelableObject);
And in your onCreate()
b.getParcelable("Key4");
I hope this helps.
Use gson to parse the json to Java. Then you can use Wagon to move the extras around with ease.
GSON:
https://github.com/google/gson
Wagon:
https://github.com/beplaya/Wagon

Categories

Resources