动态行不会出现

时间:2011-04-04 16:32:41

标签: android dynamic rows

我有一个Store.java类,我只存储2个字符串和一个整数。然后我创建一个相同的ArrayList对象。我这样做,以便我可以动态地将2个微调器和编辑文本中的值填充到ArrayList中。我想以表格格式显示这些值。此外,我还创建了Application的子类,以便可以在其他活动中轻松更新和检索ArrayList。问题是,当我点击“Bill”按钮时,动态行不会添加到表格布局中。

以下是相关代码:

(MyApp.java)

public class MyApp extends Application {

    ArrayList<store> B = null;

    public ArrayList<store> getState(){
        return B;
    }

    public void setState(ArrayList<store> B){
        this.B = 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());
        appState.setState(B);
        B = appState.getState();
        TableLayout tl = (TableLayout)findViewById(R.id.myTable);

        for(int i=0;i<B.size();i++)
        {  
            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.setId(200+i);
            b.setText(temp1.getPizzaName()); 

            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.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
            tr.addView(b2);

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

1 个答案:

答案 0 :(得分:1)

查看您的代码:

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

    store temp1=null;

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

您创建一个List,而不是将其设置为您的状态,然后再次检索它。 你想如何获得一些数据?

PS:请遵循代码约定,您的代码难以阅读。

您的变量'temp'初始化一次。您应该在每次将新实例添加到列表之前创建一个新实例。

相关问题