1

我正在从事一个项目,该项目涉及我必须与构成应用程序的所有活动共享一个对象数组列表。直到最近,我通过实现 parcelable 接口在活动之间传递列表。这方面的一个例子如下所示:

菜单项类:

ublic class MenuItem implements Parcelable {

private String name;
private double price;
private int qty;
private String info;

public MenuItem(String name, double price) {
    super();
    this.name = name;
    this.price = price;
}
public MenuItem() {
    // TODO Auto-generated constructor stub
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public double getPrice() {
    return price;
}
public void setPrice(double price) {
    this.price = price;
}

public int getQty() {
    return qty;
}
public void setQty(int qty) {
    this.qty = qty;
}
public String getInfo() {
    return info;
}
public void setInfo(String info) {
    this.info = info;
}
@Override
public String toString() {
    return name;
}


//Parcelable methods.
public static final Parcelable.Creator<MenuItem> CREATOR = new Parcelable.Creator<MenuItem>() {

    @Override
    public MenuItem createFromParcel(Parcel source) {
        return new MenuItem(source);
    }

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

public MenuItem(Parcel source) {
    this();
    readFromParcel(source);
}

private void readFromParcel(Parcel source) {
    name = source.readString();
    price = source.readDouble();
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(name);
    dest.writeDouble(price);
}

}

订单类-我将其更改为单例(保存菜单项列表)

public class Order implements Parcelable {


private int covers;
private int table;
private ArrayList<MenuItem> items;


public Order(){
    super();
    this.items = new ArrayList<MenuItem>();

}

public static final Parcelable.Creator<Order> CREATOR = new Parcelable.Creator<Order>() {

    @Override
    public Order createFromParcel(Parcel source) {
        return new Order(source);
    }

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


};

public Order(int covers, int table) {
    super();
    this.covers = covers;
    this.table = table;
    this.items = new ArrayList<MenuItem>();
}

public int getCovers() {
    return covers;
}

public void setCovers(int covers) {
    this.covers = covers;
}

public int getTable() {
    return table;
}

public void setTable(int table) {
    this.table = table;
}

public ArrayList<MenuItem> getItems() {
    return items;
}

public void setItems(ArrayList<MenuItem> items) {
    this.items = items;
}

//add to order
public void addToOrder(MenuItem m){
    items.add(m);
}

//remove from order
public void removeFromOrder(MenuItem m){
    items.remove(m);
}

public Order(Parcel source) {
    this();
    readFromParcel(source);
}

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub
    dest.writeInt(covers);
    dest.writeInt(table);
    dest.writeTypedList(items);

}

private  void readFromParcel(Parcel source) {
    // TODO Auto-generated method stub
    covers = source.readInt();
    table = source.readInt();
    items = new ArrayList<MenuItem>();
    source.readTypedList(items,  MenuItem.CREATOR);

}

}

这就是我在活动之间传递数据的方式:

myOrderBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(v.getContext(), "myOrderBtn", Toast.LENGTH_SHORT).show();
            Intent i = new Intent(v.getContext(), ViewOrderActivity.class);
            i.putExtra("data", order);
            if(!saved){
            saveOrderState();
            }
            startActivity(i);

        }
    });

并以这种方式检索:

public void getOrderData(){
    Intent i = getIntent();
    if(i != null && i.hasExtra("data")){
        order = i.getParcelableExtra("data");
        Toast.makeText(this.getApplicationContext(), "STARTERS recieved order object, covers: " + order.getCovers() + " table no: " + order.getTable() + " order size: " + order.getItems().size(), Toast.LENGTH_SHORT).show();

        if(order.getItems().size() == 0){
            //Toast.makeText(this.getApplicationContext(), "No items added to your order...", Toast.LENGTH_SHORT).show();
        }else{
            for(int j = 0; j != order.getItems().size(); j++){
                dishItems.add(order.getItems().get(j));
            }
        }

        saved = false;

    }
}

今天我学习了使用单例来全局访问数据,我想知道切换到这种数据访问方式是否是个好主意?就代码而言,它似乎更容易控制并且更整洁。

我很想知道您对此有何看法。这里的这个链接是我想要实现的模式:

http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/

我应该提到,该项目是我课程学习的一部分。我必须在几周内提交我的代码,所以我正在寻找提高效率的最佳方法。因此,如果您也发现任何看起来有问题的地方,请告诉我!

4

1 回答 1

0

几乎没有理由使用 Parcelable 在单个 Linux 进程内移动对象。我建议让您的菜单成为自定义应用程序对象的静态成员,如下所示:

public class MyApplication extends Application {
    private static final List<MenuItem> menu;
    static {
        List<MenuItem> l = new ArrayList<MenuItems>();
        l.add(new MenuItem(...));
        ...
        menu = Collections.unmodifiableList(l);
    }

    ...

    public List<MenuItems> getMenu() { return menu; }

    ....

}

确保列表是不可变的,这样您就不必担心并发问题。

在您的清单中:

<application
    android:name=".MyApplication"

...并通过以下方式访问它:

List<MenuItem> menu = ((MyApplication) getApplication()).getMenu();
于 2013-01-22T17:38:45.907 回答