以编程方式添加textViews不会打印任何内容

时间:2012-07-05 17:28:40

标签: android

我有三个列表,每个列表中的元素数量相同,我希望将所有列表的第一个元素放在一个tablerow中,然后将另一个列表中的所有列表的第二个元素放入其中, 列表是:

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>

请问我做错了什么?

2 个答案:

答案 0 :(得分: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);
        }
}

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

答案 1 :(得分: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....
    }