0

我有三个彼此具有相同数量元素的列表,我想将所有列表的第一个元素放在一个表格中,然后将所有列表的第二个元素放在另一个表格中,这些列表是:

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

所以我使用这个功能:

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

这是布局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>

请问我做错了什么?

4

2 回答 2

4

问题是您没有正确设置LayoutParamsTableRows添加TextView到布局中:

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

这是假设您的数据列表不为空。

于 2012-07-05T17:39:01.673 回答
1

添加为 TableRow 和 TextView 到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....
    }
于 2012-07-05T17:38:01.297 回答