如何将Arraylist从First Activity传递给第二个Activity?

时间:2011-04-02 08:26:34

标签: android arraylist

我正在尝试将一个arraylist从SpinPizza.java活动传递给Bill.java(使用Parcelable)。

现在的问题是,在接收活动中,我得到的arraylist会自行修改...即。它只包含所有索引中最后输入的值...我无法理解其背后的原因...我在下面发布相关代码: -

SpinPizza.java

public class SpinPizza extends Activity{

store temp= new store();

     int i=0;

ArrayList<store> B = new ArrayList<store>();

Spinner s=null,s1=null;

EditText edittext=null;

@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.drop);

          //spinners n edittext instantiation

     edittext.setOnKeyListener(new OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) {

            // If the event is a key-down event on the "enter" button

           if ((event.getAction() == KeyEvent.ACTION_DOWN) &&

                (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_DPAD_CENTER)) {

              // Perform action on key press 

       int n=Integer.parseInt(edittext.getText().toString());

        temp.setOrder(s.getSelectedItem().toString(), s1.getSelectedItem().toString(),n );

          B.add(temp);

                TextView objText=(TextView) findViewById(R.id.pl);

                TextView objText1=(TextView) findViewById(R.id.pl2);

              objText.setText(B.get(i).getPizzaName());

             objText1.setText(temp.getPizzaSize());

                 i++;



              Toast.makeText(SpinPizza.this, edittext.getText(), Toast.LENGTH_SHORT).show();

              return true;

            }

            return false;

        }

    });


      Button next1 = (Button) findViewById(R.id.bill);  

      next1.setOnClickListener(new View.OnClickListener() {

          public void onClick(View view) {


              Intent myIntent = new Intent(view.getContext(), Bill.class);


                  myIntent.putParcelableArrayListExtra("myclass",B);
                              startActivityForResult(myIntent, 0);

          }

      });

}

}

(Bill.java)

public class Bill extends Activity {
     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
this.setContentView(R.layout.calc);
ArrayList<store> B1 = new ArrayList<store>();

store temp1=null;
Intent ii= getIntent();

    B1=ii.getParcelableArrayListExtra("myclass");


TableLayout tl = (TableLayout)findViewById(R.id.myTable);

        for(int i=0;i<B1.size();i++)
        {  
            temp1=new store();
             TableRow tr = new TableRow(this);
             tr.setId(100+i);
            temp1=B1.get(i);         
             tr.setLayoutParams(new LayoutParams(
                            LayoutParams.FILL_PARENT,
                            LayoutParams.WRAP_CONTENT));

            TextView b = new TextView(this);
        b.setText(temp1.getPizzaName()); 
        b.setId(200+i);

        TextView b1 = new TextView(this);
        b1.setText(temp1.getPizzaSize());
        b1.setId(300+i);

       TextView b2 = new TextView(this);
       b2.setText(String.valueOf(temp1.getQuantity()));
     b2.setId(400+i);

        b.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
        tr.addView(b);

        b1.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        tr.addView(b1);

        b2.setLayoutParams(new LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        tr.addView(b2);     


/* Add row to TableLayout. */
tl.addView(tr,new TableLayout.LayoutParams(
          LayoutParams.FILL_PARENT,
          LayoutParams.WRAP_CONTENT));



}}}

请帮帮我。

更新

@Flo我尝试通过Application类实现这一点。但是现在我得到了Null指针异常。(我在下面的行中提到了它)

这是我修改后的代码。

SpinPizza.java - &gt;无变化......

MyApp.java

package com.Lak;

import java.util.ArrayList;

import android.app.Application;


public class MyApp extends Application {

     ArrayList<store> B = null;

          public ArrayList<store> getState(){

            return B;
          }

        }

Bill.java

public class Bill extends Activity {
     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

    this.setContentView(R.layout.calc);
    ArrayList<store> B = new ArrayList<store>();

    store temp1=null;

    MyApp appState = ((MyApp)getApplicationContext());   
    B = appState.getState();



 TableLayout tl = (TableLayout)findViewById(R.id.myTable);
         /* Create a new row to be added. */


              /* Create TEXTVIEWS to be the row-content. */






            for(int i=0;i<B.size();i++)    //NULL POINTER EXCEPTION
            {  
                temp1=new store();
                 TableRow tr = new TableRow(this);
                 tr.setId(100+i);
                temp1=B.get(i);      
                 tr.setLayoutParams(new LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));


                TextView b = new TextView(this);
            b.setText(temp1.getPizzaName()); 
            b.setId(200+i);


            TextView b1 = new TextView(this);
            b1.setText(temp1.getPizzaSize());
            b1.setId(300+i);

           TextView b2 = new TextView(this);
           b2.setText(String.valueOf(temp1.getQuantity()));
         b2.setId(400+i);

            b.setLayoutParams(new LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
            tr.addView(b);

            b1.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            tr.addView(b1);


            b2.setLayoutParams(new LayoutParams(
                    LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));

            tr.addView(b2);


    /* Add row to TableLayout. */
    tl.addView(tr,new TableLayout.LayoutParams(
              LayoutParams.FILL_PARENT,
              LayoutParams.WRAP_CONTENT));

     }}}

我认为应用程序类没有发送更新的Arraylist ...它只是发送默认的..(定义为NULL):-(

2 个答案:

答案 0 :(得分:2)

您的putParcelableArrayListExtra()方法应具有完全限定的类名 - 文档说:

  

将扩展数据添加到intent中。该   name必须包含一个包前缀,   例如应用程序   com.android.contacts将使用名称   比如“com.android.contacts.ShowAll”。

你的store类(btw类名应该以大写字母开头)是否实现了Parcelable?这是putParcelableArrayListExtra()

合同的一部分

答案 1 :(得分:1)

为什么不使用singleton类或Application类将列表传递给其他活动。可以从应用程序的任何位置访问单例或应用程序类。