android TableLayout with dynamically added cells - nothing displayed

0

I have TableLayout with dynamically added cells. Unfortunately cells are not displayed (all dynamically added part seems to be not existing when displaying layout).

First part of my layout definition:

<TableLayout 
android:id="@+id/main_activity_details_calendar_main_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

now one cell xml definition:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:layout_weight="1"
    >

    <TextView 
        android:id="@+id/main_activity_details_calendar_item_text_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxLines="1"
        android:gravity="left"
        />
    <TextView 
        android:id="@+id/main_activity_details_calendar_item_text_counter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxLines="1"
        android:gravity="right"
        />

</LinearLayout>

Now in my code I have:

public class MainActivity 
.....

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

......

calendar.onCreateView(view, inflater, container);

.....

return view;
}

protected class Calendar { //this is inner class

   protected TableLayout calendarMainGrid;
   public final static int NUMBER_OF_WEEKS = 2;
   public final static int NUMBER_OF_DAYS_IN_WEEK=7;

   public void onCreateView (View view, LayoutInflater inflater, ViewGroup container) {

      calendarMainGrid = (TableLayout) view.findViewById(R.id.main_activity_details_calendar_main_grid);

       for (int i=0; i<=NUMBER_OF_WEEKS; i++) { //we have <= as first row will be used for displaying the names of days

          TableRow newRow = new TableRow(calendarMainGrid.getContext());
          newRow.setLayoutParams (new TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT, 
                        android.widget.TableRow.LayoutParams.WRAP_CONTENT));

          for (int j=0; j<NUMBER_OF_DAYS_IN_WEEK; j++) {
              View cell = inflater.inflate(R.layout.main_activity_details_calendar_item, container, false);

               TextView t1 = (TextView) cell.findViewById(R.id.main_activity_details_calendar_item_text_date);
           TextView t2 = (TextView) cell.findViewById(R.id.main_activity_details_calendar_item_text_counter);
           t1.setText(i*NUMBER_OF_DAYS_IN_WEEK+j);
           t2.setText(i*NUMBER_OF_DAYS_IN_WEEK+j);
               newRow.addView(cell);


    }

       calendarMainGrid.addView(newRow, new TableLayout.LayoutParams(android.widget.TableLayout.LayoutParams.MATCH_PARENT, 
                        android.widget.TableLayout.LayoutParams.WRAP_CONTENT));
            }
        }

And as I said nothing from TableLayout is displayed (other statically set elements in xml file of the same layout are correctly displayed).

Moreover LogCat displays the following warnings:

01-29 22:03:31.599: W/ResourceType(8561): No package identifier when getting value for resource number 0x00000000

and next warnings for numbers until 14.

I would be grateful for help. Thank you in advance!

Btw, I really don't understand why Android doesn't provide any calendar ready from the shelf with customized view on one cell..... Now instead of adding customized calendar in 5 minutes, I'm spending hours in creating calendar view from tablelayout (and I've also discovered that in my case I'm not allowed to use GridView as I've already have one ScrollView in my Layout).

android
android-layout
android-resources
android-tablelayout
android-inflate
asked on Stack Overflow Jan 29, 2014 by user2707175 • edited Jul 3, 2018 by Vadim Kotov

2 Answers

0

android:layout_width="match_parent"

try changing this to: android:layout_width="wrap_content"

answered on Stack Overflow Jan 29, 2014 by knightsb
0

Two errors:

It should be:

View cell = inflater.inflate(R.layout.main_activity_details_calendar_item, null);

and next:

t1.setText(Integer.toString(i*NUMBER_OF_DAYS_IN_ONE_WEEK+j));

Otherwise there is not automatic conversion as there is also setText with int parameter which means in this case ressource id.

Oh, and I spent so much time on those "stupid" bugs....

Thus the problem is solved!

answered on Stack Overflow Jan 29, 2014 by user2707175

User contributions licensed under CC BY-SA 3.0