Android ParceLable,在对象传回后处理

时间:2015-04-16 21:29:26

标签: java android android-intent parcelable

嗨,我只是说我是一个初学的Android开发人员,我的代码可能有点乱,我也感谢任何人都可以给我的任何帮助,我已经使用本教程实现了parcelable http://www.javacodegeeks.com/2014/01/android-tutorial-two-methods-of-passing-object-by-intent-serializableparcelable.html

我有两个活动活动A和活动B,第一个显示自定义对象(属性)列表,Property类实现parcelable

活动A有一个自定义列表,我想动态添加对象。在这个活动中,我有一个按钮,打开活动B,我输入创建属性对象所需的信息,我想将其发送回活动A,以便添加到列表中。

在我实现parcelable之前,我能够创建没有问题的对象,但它被卡在活动B中,需要在实现parcelable后添加到活动A的列表中,当我第一次尝试打开活动时我得到一个错误因为我在活动A中的onCreate方法中添加了Property mProperty = getIntent().getParcelableExtra(AddProperty.PAR_KEY);,在活动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);

}

}

该职业课程也有充分的吸气者和制定者。

createViewing方法在活动B上,是用于创建对象并将对象发回的

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();
        }

这是活动A的onCreate方法

 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();

        }
    }));

0 个答案:

没有答案