【问题标题】:Update an object passed through Parcelable intent更新通过 Parcelable 意图传递的对象
【发布时间】:2015-07-30 22:02:47
【问题描述】:

我是 Android 新手,我还在学习。我目前有一个 ListView 允许您单击一个项目。单击一个项目将打开一个新意图,显示有关该项目的额外信息。

我遇到的问题是弄清楚如何将更新后的值返回到我的自定义对象中,并在正确的索引处更新数组中的值。

例如: 我将添加一个项目并将其数量设置为 2。这将出现在我的 ListView 中。伟大的。我决定我需要 3 而不是 2,所以我单击该项目以打开新活动,看到 2 的数量,将其更新为 3 并点击保存。在保存单击时,我想返回我的列表视图并在那里显示更新的数量值,并在索引处的数组中更新。

段代码:

ItemList类中listview的Onclick方法

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        //@Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                                int arg2, long arg3) {

            bean = (CustomObject) listview.getItemAtPosition(arg2);

            Intent in1 = new Intent(Itemlist.this, SingleItem.class);
            in1.putExtra("ActivityObject", bean);
            startActivity(in1);
        }
    });

在我的 ItemList 类中的数组中添加一个项目。这包含列表视图。

else {
                objects.add(new CustomObject(roomname.getText().toString() + " - " + resultSet.get(namecount), resultSet.get(partno), itemq, "$" + resultSet.get(rrpcol), resultSet.get(glcode), resultSet.get(desc)));
                adapter.notifyDataSetChanged();

单项类

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_singleitem);

    siname = (TextView) findViewById(R.id.siname);
    sipartno = (TextView) findViewById(R.id.sipartno);
    siquantity = (EditText) findViewById(R.id.siq);
    sirrp = (EditText) findViewById(R.id.sirrp);
    siglcode = (TextView) findViewById(R.id.siglcode);
    sidesc = (EditText) findViewById(R.id.sidesc);
    update = (Button) findViewById(R.id.siupdate);

    Bundle b = getIntent().getExtras();
    CustomObject itemInfo = b.getParcelable("ActivityObject");

    siname.setText(itemInfo.getItemName());
    sipartno.setText(itemInfo.getItemPartNo());
    siquantity.setText(itemInfo.getItemQuantity());
    sirrp.setText(itemInfo.getItemPrice());
    siglcode.setText(itemInfo.getItemGLCode());
    sidesc.setText(itemInfo.getItemDesc());
}

自定义对象类

public class CustomObject implements Parcelable {

private String itemName;
private String itemPartNo;
private String itemQuantity;
private String itemPrice;
private String itemGLCode;
private String itemDesc;

public CustomObject(Parcel source){
        /*
         * Reconstruct from the Parcel
         */
    //Log.v(TAG, "ParcelData(Parcel source): time to put back parcel data");
    //id = source.readInt();
    itemName = source.readString();
    itemPartNo = source.readString();
    itemQuantity = source.readString();
    itemPrice = source.readString();
    itemGLCode = source.readString();
    itemDesc = source.readString();

}

public CustomObject(String prop1, String prop2, String prop3, String prop4, String prop5, String prop6) {
    this.itemName = prop1;
    this.itemPartNo = prop2;
    this.itemQuantity = prop3;
    this.itemPrice = prop4;
    this.itemGLCode = prop5;
    this.itemDesc = prop6;
}

public String getItemName() {
    return itemName;
}

public String getItemPartNo() { return itemPartNo; }

public String getItemQuantity() {
    return itemQuantity;
}

public String getItemPrice() {
    return itemPrice;
}

public String getItemGLCode() {return itemGLCode;}

public String getItemDesc() {return itemDesc;}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(itemName);
    dest.writeString(itemPartNo);
    dest.writeString(itemQuantity);
    dest.writeString(itemPrice);
    dest.writeString(itemGLCode);
    dest.writeString(itemDesc);
}

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
    public CustomObject createFromParcel(Parcel in) {
        return new CustomObject(in);
    }

    public CustomObject[] newArray(int size) {
        return new CustomObject[size];
    }
};

}

我希望能够更改 SingleItem 类中的数量,单击“更新”按钮,然后让它在 itemlist 类中加载项目列表中的更新值。

【问题讨论】:

    标签: android arrays android-intent indexing parcelable


    【解决方案1】:

    Fragments 与您自己为活动定义的回调接口一起使用会更有效。但是,如果您想采用 Activity 方法,请使用 startActivityForResult() 并让您的详细信息 Activity 发回带有任何更新对象内容的结果 Intent

    【讨论】:

    • 谢谢你。没有考虑碎片。现在我要去读了:)
    猜你喜欢
    • 1970-01-01
    • 2012-10-04
    • 2018-09-11
    • 2015-03-17
    • 2012-11-22
    • 1970-01-01
    • 2011-11-15
    • 2013-10-13
    • 2015-05-30
    相关资源
    最近更新 更多