1

Hi just to state i am a beginner android developer and my code may be a bit messy i also appreciate any help anyone can give me, i have implemented parcelable using this tutorial http://www.javacodegeeks.com/2014/01/android-tutorial-two-methods-of-passing-object-by-intent-serializableparcelable.html

i have Two activities activity A and activity B, the first displays a list of custom objects (properties) and the Property class implements parcelable

Activity A has a custom list and I want to add objects dynamically. In this activity I have a button that opens activity B where i input the information required to created a property object which i want to send back to activity A to be added to the list.

Before I implemented parcelable i was able to create the object with no issue but it was stuck in activity B and needs to be added to the list in activity A after implementing parcelable when i first try to open activity A i get an error that crashes my app i think it's because i've added Property mProperty = getIntent().getParcelableExtra(AddProperty.PAR_KEY); in the onCreate method in activity A which looks for an intent before ones been created in Activity B

public class Property implements Parcelable {
// have quit alot of fields so took them out to save space

public Property()
{

}
public Property(String postCode, String address, String county,int noRoom, int askPrice,
                String eName,String agentName,String agentNumber, String time) {
    this.postCode = postCode;
    this.addressFirsLine = address;
    this.county = county;
   setNumberOfRoom(noRoom);
    //numberOfRoom = 2;
    setAskingPrice(askPrice);
    //askingPrice = 0;
    //setCurrentOffer(currentOff);
    currentOffer = 0;
    //setAgreedPrice(agreedPrice);
    agreedPrice = 0;
  // setRefurbCost(refurb);
    //refurbCost = 2555;
    setEstateAgent(eName,agentNumber ,agentName);
   // estateAgent = null;
    condition = false;
     setTime(time);


}

 public static final Creator<Property> CREATOR = new Creator<Property>() {


    @Override
    public Property createFromParcel(Parcel source) {
        Property mProperty = new Property();
        mProperty.postCode = source.readString() ;
        mProperty.addressFirsLine = source.readString();
        mProperty.county =source.readString() ;
        mProperty.numberOfRoom = source.readInt();
        mProperty.askingPrice = source.readInt();
        mProperty.agentName = source.readString();
        mProperty.agentNumber = source.readString();
        mProperty.eName = source.readString();
        return mProperty;
    }

    @Override
    public Property[] newArray(int size) {
        return new Property[size];
    }
};

@Override
public int describeContents() {
    return 0;
}




@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(postCode);
    dest.writeString(addressFirsLine);
    dest.writeString(county);
    dest.writeInt(numberOfRoom);
    dest.writeInt(askingPrice);
    dest.writeString(agentName);
    dest.writeString(agentNumber);
    dest.writeString(eName);
    dest.writeString(time);

}

}

the propery class also has full getters and setters.

the createViewing method is on activity B and is whats used to create the object and send the object back

public void CreateViewing(View view) {
    String  strPostCode,strAddressFirsLine,strCounty,strEstateAgent,strAgentName,strAgentPhone,strTime ;
    int roomNO ;
    int askingPrice2 ;
    try{
    strPostCode = postCode.getText().toString();
    strAddressFirsLine=addressFirsLine.getText().toString();
    strCounty = county.getText().toString();
    roomNO = Integer.parseInt(roomNumber.getText().toString());
    askingPrice2 = Integer.parseInt(askingPrice.getText().toString());
    strEstateAgent=estateAgent.getText().toString();
    strAgentName=agentName.getText().toString() ;
    strAgentPhone=agentPhone.getText().toString() ;
    strTime =time.getText().toString() ;

    Property mProperty = new Property(strPostCode, strAddressFirsLine,
            strCounty ,roomNO,askingPrice2,strEstateAgent ,strAgentName,strAgentPhone,
            strTime ) ;

    String r = mProperty.toString() ;

        Intent mIntent = new Intent(this,ViewingSchedule.class);
        Bundle mBundle = new Bundle();
        mBundle.putParcelable(PAR_KEY,mProperty);
        mIntent.putExtras(mBundle);
        startActivity(mIntent);

    Toast.makeText(AddProperty.this, r, Toast.LENGTH_SHORT).show();

} catch (Exception e) {
            e.printStackTrace();
        }

this is the onCreate Method for activity A

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_viewing_schedule);
    Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
   Property mProperty = getIntent().getParcelableExtra(AddProperty.PAR_KEY);

    Toast.makeText(ViewingSchedule.this,mProperty.toString(),Toast.LENGTH_SHORT).show();
     Property[] propertyList = {new Property("SG1 1LS", "24 CrossGates", "Hertfordshire",2, 200000,"Connels","becky","078123456","9:00")};
      //propertyList = mProperty ;

    ListView listView1 = (ListView) findViewById(R.id.listView);

    ArrayAdapter adapter = new myAdapter2(this,propertyList);

    listView1.setAdapter(adapter);
    listView1.setOnItemClickListener((new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String itemSelected = "You selected " +
                                                String.valueOf(parent.getItemAtPosition(position));

                                Toast.makeText(ViewingSchedule.this, itemSelected, Toast.LENGTH_SHORT).show();

        }
    }));
vgarzom
  • 153
  • 10
  • I think i need to create a method that's called if a intent has been sent to retrives the object but im not sure how to – BeginnerJavaDev Apr 16 '15 at 21:46
  • Maybe it's because the first time you launch your app, you are not passing data with the key PAR_KEY. So if what you want is to create the new Property object and get back to Activity A, you have to start activity B with startActivityForResult and then use onActivityResult on Activity A. – vgarzom Apr 16 '15 at 22:06
  • Only call `getIntent().getParcelableExtra()` if `getIntent().getExtras()` is not null. – Daniel Nugent Apr 16 '15 at 22:08
  • @vgarzom Hi how would i do this? im sorry im still pretty new with android, – BeginnerJavaDev Apr 16 '15 at 22:14
  • @DanielNugent would i do this with something like if (getIntent().getExtras() != null) { getIntent().getParcelableExtra() } – BeginnerJavaDev Apr 16 '15 at 22:14
  • Yes, that would work, it might fix the crash that you are currently getting. Also, take a look at this answer for info on `startActivityForResult()` http://stackoverflow.com/questions/10407159/how-to-manage-start-activity-for-result-on-android/10407371#10407371 – Daniel Nugent Apr 16 '15 at 22:19
  • The If statement has fixed the crash thank you :) now i just need to work out how to add the object to the list but i can hopefully work that out myself :D – BeginnerJavaDev Apr 16 '15 at 22:22
  • thanks for the help daniel been awesome :) checking that tutorial now – BeginnerJavaDev Apr 16 '15 at 22:23

0 Answers0