Arraylist 只检索最后一个值

发布于 2024-10-29 04:06:29 字数 2962 浏览 1 评论 0原文

您好,我正在使用 Parcelable 将数组列表从一个活动传递到另一个活动。

现在的问题是,当我单击 BILL 时,所有行都将填充数组列表的最后一个值。所以我想我无法检索调用活动中的整个数组列表...

这是相关代码..

SpinPizza.java

public class SpinPizza extends Activity{
store temp= new store();

     int i=0;

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


//oncreate method



  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++;




    });

      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=new store();

Bundle bj=getIntent().getExtras();


    B1=bj.getParcelableArrayList("myclass");

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



                      for(int i=0;i<B1.size();i++)
        { 
             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);



tl.addView(tr,new TableLayout.LayoutParams(
          LayoutParams.FILL_PARENT,
          LayoutParams.WRAP_CONTENT));


 }}

}

请帮忙。

Hi I am passing an arraylist from one activity to another using Parcelable..

Now the problem is that when I click on BILL, all the rows are populated with the last value of the arraylist..So I guess I am not able to retreive the whole arraylist in the calling activity...

Here is the relevant code..

SpinPizza.java

public class SpinPizza extends Activity{
store temp= new store();

     int i=0;

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


//oncreate method



  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++;




    });

      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=new store();

Bundle bj=getIntent().getExtras();


    B1=bj.getParcelableArrayList("myclass");

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



                      for(int i=0;i<B1.size();i++)
        { 
             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);



tl.addView(tr,new TableLayout.LayoutParams(
          LayoutParams.FILL_PARENT,
          LayoutParams.WRAP_CONTENT));


 }}

}

PLEASE HELP.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南笙 2024-11-05 04:06:29

我看不到 temp 的创建,也看不到 temp 修饰符行 temp.setOrder(s.getSelectedItem().toString) 中使用的迭代(), s1.getSelectedItem().toString(),n);。看来您没有在那里使用 i 这就是为什么您有相同的记录。

I can't see the creation of temp, nor i can see the iteration using in your temp modifier line temp.setOrder(s.getSelectedItem().toString(), s1.getSelectedItem().toString(),n);. It seems that you don't use i there that's why you have equal records.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文