Question

Edit: as Blumer pointed out, I was not adding the items to the table, so that this question appeared just because I was careless and I didn't see my mistake.

I am trying to create a dynamic TableLayout, as I have to receive results from the server and add rows based on the results, but the table is not updating. (Also, the TableLayout already has 1 initial row, the header row).

This is my code:

    Room[] rooms = State.rooms;
    TableLayout tblBookDetails = (TableLayout) findViewById(R.id.tblbookdetails);
    for(int i = 0; i < rooms.length; i++) {
        TableRow tr = new TableRow(this);
        tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        LayoutParams layout_wrapwrap = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layout_wrapwrap.rightMargin = 10; //TODO: Convert into DP
        Resources res = getResources();

        TextView txt1 = new TextView(this);
        txt1.setLayoutParams(layout_wrapwrap);
        txt1.setTextColor(res.getColor(android.R.color.black));
        txt1.setText(rooms[i].name);
        TextView txt2 = new TextView(this);
        txt2.setLayoutParams(layout_wrapwrap);
        txt2.setTextColor(getResources().getColor(android.R.color.black));
        txt2.setText(rooms[i].price + " " + rooms[i].currency);
        EditText edit1 = new EditText(this);
        edit1.setLayoutParams(layout_wrapwrap);
        //Must use deprecated method, since support library does not provide for this.
        edit1.setBackgroundDrawable(res.getDrawable(android.R.drawable.edit_text));
        edit1.setEms(3);
        edit1.setInputType(InputType.TYPE_CLASS_NUMBER);
        EditText edit2 = new EditText(this);
        edit2.setLayoutParams(layout_wrapwrap);
        //Must use deprecated method, since support library does not provide for this.
        edit2.setBackgroundDrawable(res.getDrawable(android.R.drawable.edit_text));
        edit2.setEms(3);
        edit2.setInputType(InputType.TYPE_CLASS_NUMBER);
        Spinner spinner = new Spinner(this);
        layout_wrapwrap.rightMargin = 0;
        spinner.setLayoutParams(layout_wrapwrap);

        Integer[] numbers = new Integer[rooms[i].count];
        for(int j = 0; j < numbers.length; j++) {
            numbers[j] = i + 1;
        }
        ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(
                BookActivity.this, R.layout.spinner_textview, numbers);
        spinner.setAdapter(adapter);
        tblBookDetails.addView(tr);
    }
    //Another exquisite beauty of Java.
    Log.d("USR", Integer.valueOf(tblBookDetails.getChildCount()).toString());
    tblBookDetails.invalidate();
    tblBookDetails.refreshDrawableState();

To prevent any confusion, the Room[] array is just a simple property-holder class. This code looks enormously convoluted, and the table is not updating. I've searched quite a bit on the Internet, and I could not find any solution for this problem.

Thank you in advance.

Was it helpful?

Solution

I see where you add tr to tblBookDetails, but I don't see anywhere where you put txt1, txt2, edit1, etc. in tr. Try adding those views to the row, and I think that should get you there, because right now you appear to be adding the TableRow, but there's nothing in it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top