0

在此处输入图像描述

创建表后,不会填充行。我所拥有的只是列标题“fName”。fName.setText 似乎不起作用。我检查以确保completedWord.get(0)有一个值,并且确实如此。布局可能有问题,但我不确定。有什么建议么?

public void displayList () {
   int rowCount = completedWords.size();
   Log.d("Fill table", "rowCount = " + rowCount);
   TableLayout table = (TableLayout) this.findViewById(R.id.tablelayout);
   for (int i = 0; i <rowCount; i++) {
     fillRow(table, i);
   }
}

public void fillRow(TableLayout table, int noRow) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View fullRow = inflater.inflate(R.layout.activity_main, null, false);
    TextView fName = (TextView) fullRow.findViewById(R.id.fName);
    System.out.println("Table should read " + completedWords.get(noRow));
    fName.setText(completedWords.get(noRow));
    fName.setId(noRow + 1);
    table.addView(fullRow);
}

XML 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:paddingBottom="@dimen/activity_vertical_margin"
     android:paddingLeft="@dimen/activity_horizontal_margin"
     android:paddingRight="@dimen/activity_horizontal_margin"
     android:paddingTop="@dimen/activity_vertical_margin"
     tools:context=".MainActivity" 
     android:gravity="center_horizontal"
     android:background ="#268496" >


 <EditText
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:id="@+id/input"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:layout_toRightOf="@+id/prefix"
    android:textSize="12pt"
    android:maxLength="1"
    android:typeface="sans" />

<TextView
    android:id="@id/prefix"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF"
    android:textIsSelectable="true"
    android:textSize="12pt"
    android:typeface="sans" />

<TableLayout
    android:id="@+id/tablelayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:shrinkColumns="0"
    android:gravity="center_horizontal"
    android:layout_below="@id/prefix" >

        <TextView
        android:id="@+id/fName"
        android:layout_marginTop="17dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"
        android:textIsSelectable="true"
        android:textSize="7pt"
        android:typeface="sans"
        android:text="Found words" />
 </TableLayout>
</RelativeLayout>
4

2 回答 2

0

不要膨胀R.layout.activity_main来获取 TextView 并添加到表中您可以动态创建 TextView 并将其添加到表中。

public void fillRow(TableLayout table, int noRow) {
TableRow fullRow = new TableRow(this);
TextView fName = new TextView(this);
System.out.println("Table should read " + completedWords.get(noRow));
fName.setText(completedWords.get(noRow));//or u can try completedWords.elementAt(noRow)
fName.setId(noRow + 1);
table.addView(fullRow);
}
于 2013-05-10T15:47:31.407 回答
0

尝试:

fName.setText(String.valueOf(completedWords.get(noRow)));

编辑:

我尝试了类似于你的代码的东西:

String[] words=new String[]{"this","is","an","example"};

public void displayList () {
           int rowCount = words.length;
           Log.d("Fill table", "rowCount = " + rowCount);
           TableLayout table = (TableLayout) this.findViewById(R.id.tablelayout);
           for (int i = 0; i <rowCount; i++) {
             fillRow(table, i);
           }
        }

        public void fillRow(TableLayout table, int noRow) {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View fullRow = inflater.inflate(R.layout.activity_main, null, false);
            TextView fName = (TextView) fullRow.findViewById(R.id.fName);
            //System.out.println("Table should read " + completedWords.get(noRow));
            fName.setText(String.valueOf(words[noRow]));
            //fName.setId(noRow);
            table.addView(fullRow);
        }

结果是这样的:

在此处输入图像描述

我认为你应该重新审视你的行布局。

于 2013-05-10T15:25:34.780 回答