Search code examples
androidandroid-layoutandroid-event

android create textViews in tableRows programmatically


I have these three lists with the same number of elements

List<String>competitoinsIDs = new LinkedList<String>();
List<String>marks = new LinkedList<String>();
List<String>numOfQuestions = new LinkedList<String>();

I want to put the first element of each list in a tablerow , then the second element of each list in another tablerow, would you help me please, this is the xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableLayout
        android:id="@+id/tlMarksTable"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5dip"
            android:weightSum="2" >

            <TextView
                android:id="@+id/tvMarksCompetitionID"
                android:layout_weight="1"
                android:text="Competition"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/tvMarksMarks"
                android:layout_weight="1"
                android:text="Marks"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/tvMarksQuestionsNum"
                android:layout_weight="1"
                android:text="Questions"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </TableRow>
    </TableLayout>

</FrameLayout>

Solution

  • First change your XML file to:

       <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <TableLayout
            android:id="@+id/tlMarksTable"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
    
        </TableLayout>
        </FrameLayout>
    

    We'll add all the content dynamically.

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        List<String>competitoinsIDs = new LinkedList<String>();
        List<String>marks = new LinkedList<String>();
        List<String>numOfQuestions = new LinkedList<String>();
    
     //make sure that the lists contain data or else display will be blank screen
    
        TableRow.LayoutParams  params1=new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1.0f);
        TableRow.LayoutParams params2=new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
        TableLayout tbl=(TableLayout) findViewById(R.id.tlMarksTable);
        for(int ctr=0;ctr<marks.size();ctr++)
        {
        //Creating new tablerows and textviews
        TableRow row=new TableRow(this);
        TextView txt1=new TextView(this);
        TextView txt2=new TextView(this);
        TextView txt3=new TextView(this);
        //setting the text
        txt1.setText(competitoinsIDs.get(ctr));
        txt2.setText(marks.get(ctr));
        txt3.setText(numOfQuestions.get(ctr));
        txt1.setLayoutParams(params1);
        txt2.setLayoutParams(params1);
        txt3.setLayoutParams(params1);
        //the textviews have to be added to the row created
        row.addView(txt1);
        row.addView(txt2);
        row.addView(txt3);
        row.setLayoutParams(params2);
        tbl.addView(row);
        }
    
    }