0

在我的一项活动中,当从 Intent 接收到包时,我创建了一个线性布局和一些其他小部件。目前,每次我回到那个 Activity 时,那个 Layout 都会被覆盖。如何在不重写代码的情况下每次都创建一个新的布局?

代码:

public class FrontPageActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frontpage);


        Bundle bundle = this.getIntent().getExtras();
        try{

            String size = bundle.getString("size");
            int toppcount = bundle.getStringArrayList("toppings").toArray().length;




            LinearLayout container = (LinearLayout)findViewById(R.id.container);
            TextView t = new TextView(this);


            TextView tprice = new TextView(this);
            tprice.setGravity(Gravity.RIGHT);


            LinearLayout inner = new LinearLayout(this);
            LinearLayout.LayoutParams innerparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            inner.setLayoutParams(innerparams);
            inner.setBackgroundDrawable(getResources().getDrawable(R.drawable.background));
            inner.setPadding(10,10,10,10);

            if(toppcount == 0){
                t.setText(size+" Cheese Pizza");
            }
            else{
                t.setText(size+" "+toppcount+" Topping Pizza");
            }



            tprice.setText(getPrice(size, toppcount)+"");



            tprice.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
                    LinearLayout.LayoutParams.FILL_PARENT));


            container.addView(inner);
            inner.addView(t);
            inner.addView(tprice);
        }
        catch(NullPointerException e){


        }
        final Intent sender = new Intent(this.getApplicationContext(), OrderPizzaActivity.class);


        Button badd = (Button)findViewById(R.id.buttonaddpizza);



        badd.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {


                startActivityForResult(sender, 0);

            }
        });


    }
4

2 回答 2

0

听起来您需要创建一个数据结构来保存 LinearLayout,或者为每次添加它们提供一个 ViewGroup 容器。

当前,您正在创建、修改和覆盖块LinearLayout中的相同内容try{} catch(){}。我猜这就是它不断覆盖的原因。

于 2012-06-06T21:38:40.043 回答
0

据我了解,您在“最终订单”中添加了新的“选项”。每次添加额外的浇头时,您都会创建一个布局并用特定数据填充它。你希望它是又名 OrderList。如果这个应用程序是关于什么的,你可以有一个应用程序级别的变量 myOrder:List。在 FrontPageActivity 中添加浇头 (topping = new Order()) 并阅读列表。建议您为订单设置单独的布局。循环遍历订单,用数据填充布局并在 Activity 容器中膨胀。

伪思想:

MyApplication extends Application {
    private static List<Order> mOrderList = null;

    public MyApplication(){
        mOrderList = new ArrayList<Order>();
    }

    public static List<Order> getOrderList(){
        return mOrderList();
    }

    public static void addOrder(Order order) {
        mOrderList.add(order);
    }
} 

选项活动:

MyApplication.add(order);
MyApplication.add(order);

FrontPageActivity

foreach(Order order : MyApplication.getOrderList()) {
   // fill layout with order data
   // inflate orderLayout into container
}
于 2012-06-06T21:55:25.290 回答