Lakshay Gupta
Lakshay Gupta

Reputation: 73

Dynamic rows don't appear

I have a class Store.java where I simply store 2 strings and an integer. Then I create an ArrayList object of the same. I do this so that I can populate the values from 2 spinners and an edit text into the ArrayList dynamically. I want to get these values displayed in a tabular format. Also I've created a subclass of Application so that the ArrayList can be easily updated and retrieved in other Activities. The problem is that when I click on the 'Bill' button the dynamic rows are not added to the table layout.

Here is the relevant code:

(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));
        }
    }
}

Upvotes: 1

Views: 180

Answers (1)

Marcos Vasconcelos
Marcos Vasconcelos

Reputation: 18276

Take a look to your code:

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

    store temp1=null;

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

You create a List, than set it as your state, then retrieve it again. How do you want to has some data on it?

PS: Please follow the code conventions, you code is hard to read.

Your variable 'temp' is initialized once. You should make a new instance every time before add it to the List.

Upvotes: 1

Related Questions