0

i have three lists with the same number of elements in each other, i want to put first element of all lists in a tablerow, then second element of all lists in another tablerow, the lists are :

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

so i use this function:

private void addTextViews() {
        // TODO Auto-generated method stub
        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));
        //the textviews have to be added to the row created
        row.addView(txt1);
        row.addView(txt2);
        row.addView(txt3);
        tbl.addView(row);
        }
    }

and this is the layout 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>

what am i doing wrong please ?

3
  • Are you sure for loop is being called? Did you check by debugging? Commented Jul 5, 2012 at 17:33
  • i don't know how to debug , would u tell me please? Commented Jul 5, 2012 at 17:37
  • 1
    @tottiroma In this case you could use Log.d(String tag, String message) inside the loop. This would print something at the logcat. Commented Jul 5, 2012 at 17:40

2 Answers 2

4

The problem is that you don't set proper LayoutParams on the TableRows and TextView that you add to the layout:

private void addTextViews() {
        TableLayout tbl=(TableLayout) findViewById(R.id.tlMarksTable);
        for(int ctr=0;ctr<marks.size();ctr++) {
            TableRow row=new TableRow(this);
            TableLayout.LayoutParams tlp = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
            TextView txt1=new TextView(this);
            TableRow.LayoutParams text1lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT); 
            TextView txt2=new TextView(this);
            TableRow.LayoutParams text2lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT); 
            TextView txt3=new TextView(this);
            TableRow.LayoutParams text3lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT); 

            txt1.setText(competitoinsIDs.get(ctr));
            txt1.setLayoutParams(text1lp);
            txt2.setText(marks.get(ctr));
            txt2.setLayoutParams(text2lp);
            txt3.setText(numOfQuestions.get(ctr));
            txt3.setLayoutParams(text3lp);
            row.addView(txt1);
            row.addView(txt2);
            row.addView(txt3);
            row.setLayoutParams(tlp);
            tbl.addView(row);
        }
}

This is assuming that your data lists aren't empty.

Sign up to request clarification or add additional context in comments.

11 Comments

still nothing print on the screen , and i am sure the lists is not empty becuase i print it
@tottiroma Are you sure the addTextViews method is being called?
@tottiroma Yes, you must use it, I forgot to add it in my code. I've edited the answer.
@tottiroma You don't set the content view in the activity so when you search for the TableLayout it returns null. In the onCreate method set the content view with setContentView(R.layout.layout_containing_the_tablelayout);(the layout file from your question?!?!) after the line super.onCreate(savedInstanceState);
@tottiroma Your first TableRow has some padding set on it in the xml layout so you should set the same padding on the TableRow that you create in code(use the method setPadding on row). Also, remove the weightSum and the layout_weight attributes from the first row and set android:stretchColumns="*" on your TableLayout if you want equal width columns.
|
1

ADD as TableRow and TextView To TableLayout :

for(int ctr=0;ctr<marks.size();ctr++)
        {       
       //YOUR CODE....
        TableRow row=new TableRow(this);
        row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        TextView txt1=new TextView(this);
        txt1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        TextView txt2=new TextView(this);
        txt1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        TextView txt3=new TextView(this);
        txt3.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        //YOUR CODE....
    }

4 Comments

oh ur answer is like the previus answer ,and doesn't print any thing on screen :(
@tottiroma :what do you mean by previus answer i give you answer first ok and make sure you are calling addTextViews method for adding rows
this is my oncreate competitoinsIDs = new LinkedList<String>(); marks = new LinkedList<String>(); numOfQuestions = new LinkedList<String>(); competitoinsIDs.add("4"); marks.add("2"); numOfQuestions.add("4"); addTextViews(); i got null pointer exception, thank you
i didn't knew that u was the first, sorry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.