0

enter image description here

Upon creating the table, the rows do not populate. All I have is the column title 'fName'. The fName.setText does not seem to work. I checked to make sure completedWord.get(0) has a value and it does. There could be a problem with the layout but I'm not sure. Any suggestions?

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 file:

<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>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ono
  • 2,984
  • 9
  • 43
  • 85

2 Answers2

0

Try:

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

EDIT:

I tryied something similar to your code:

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

and the result is this:

enter image description here

I think you should revisit your row layout.

GVillani82
  • 17,196
  • 30
  • 105
  • 172
  • In this way: fName.setId(noRow + 1); you are changing the id of the textView you are referring to. So in the next step, when you try to refer to R.id.fName, you fail. I suggest you to use two step. The former for assign id and the latter for setText. (remember to use different id, considering other view in your activity). This may help you: http://stackoverflow.com/questions/1714297/android-view-setidint-id-programmatically-how-to-avoid-id-conflicts – GVillani82 May 10 '13 at 15:36
  • I'm getting any actual errors. How would I check why it's not being displayed. I tried just using noRow instead of noRow + 1. Still nothing. – ono May 10 '13 at 16:04
  • I have tried your code, just replacing fName.setText(completedWords.get(noRow)) with fName.setText(String.valueOf(noRow)); where noRow is an int. Ths works for me. – GVillani82 May 10 '13 at 18:04
  • instead of words did you mean completedWords? and then do completedWords.get(noRow)? – ono May 10 '13 at 18:25
  • yes, I define words as array of String. As edited (words=new String[]{"this","is","an","example"};) This works for you? – GVillani82 May 10 '13 at 19:18
  • Unfortunately not. I'll need to look at it some more. I also just want the text listed, not the EditText view. I can fix that myself though. – ono May 10 '13 at 20:25
  • This may help you: http://developer.android.com/guide/topics/ui/layout/listview.html – GVillani82 May 10 '13 at 20:34
0

Don't inflate R.layout.activity_main to get a TextView and add to the table You can create a TextView dynamically and add it to the table.

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);
}
bakriOnFire
  • 2,685
  • 1
  • 15
  • 27