2016-07-04 10 views
0

Ich bin neu in Android. Ich habe versucht, TableRows zu einem TableLayout hinzuzufügen, aber es funktioniert nicht. Ich habe keine Ahnung, warum es nicht zeigt. Ich habe versucht, aus dem Internet zu schauen, aber scheint, dass mein Problem nicht das gleiche ist. Ich habe keine Ausnahmen oder Fehler im Logcat. Ich habe versucht zu debuggen und herausgefunden, dass die Ansichten hinzugefügt werden, aber aus irgendeinem Grund werden sie nicht angezeigt. Hat jemand eine Idee dazu? Danke im Voraus. Mein Code:Programmatisch hinzugefügt TableRows zu einem TableLayout nicht angezeigt

private void addInfosToTable(){ //this is called in onCreate() function 
//these are just dummy infos i want to test the how the table works 
    Cell cell = new Cell("1234",5000,3000); 
    cell.setStatus(Cell.Status.COMPLETE); 
    Cell cell1 = new Cell("1234",5000,3000); 
    cell1.setStatus(Cell.Status.FAILED); 
    addCellRowToTable(cell); 
    addCellRowToTable(cell1); 
} 

    private void addCellRowToTable(Cell cell){ 
    //init cell info 
    String cellID = cell.getCellID(); 
    double targetSpeed = cell.targetSpeed(); 
    double targetPoint = cell.targetPoint(); 
    Cell.Status status = cell.getStatus(); 

    //create tableRow 
    TableRow tableRow = new TableRow(this); 
    tableRow.addView(getTextView(cellID,true)); 
    tableRow.addView(getTextView("n.a"+"/ "+"n.a",false)); 
    tableRow.addView(getTextView(targetSpeed+"/ "+targetPoint,false)); 
    tableRow.addView(getStatusView(status)); 
    tableRow.setPadding(0,R.dimen.table_marginVertical,R.dimen.table_marginVertical,0); 
    tableRow.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT)); 

    //add tableRow to tableLayout 
    this.cellTable.addView(tableRow); 

} 
private TextView getTextView(String text, boolean isFirst){ 
    TextView textView = new TextView(this); 
    textView.setTextSize(R.dimen.table_textSize); 
    textView.setText(text); 
    textView.setTextColor(ContextCompat.getColor(this,R.color.black)); 
    if (isFirst){ 
     textView.setPadding(R.dimen.table_marginVertical,0,0,0); 
    } 
    return textView; 

} 
private ImageView getStatusView(Cell.Status status){ 
    ImageView imageView = new ImageView(this); 
    switch (status){ 
     case COMPLETE: 
      imageView.setImageResource(R.drawable.success); 
      return imageView; 
     default: 
      imageView.setImageResource(R.drawable.failed); 
      return imageView; 
    } 
} 

Meine Tabelle hat 4 Spalten, und es sieht wie folgt aus:

<?xml version="1.0" encoding="utf-8"?> 
<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" 
tools:context="superapp.networkapp.MainActivity" 
android:focusableInTouchMode="true"> 

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    android:name="com.google.android.gms.maps.MapFragment" 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 
<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="false" 
    android:layout_alignParentEnd="false"> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/warning_box" 
     android:padding="10dp" 
     android:layout_margin="3dp" 
     android:weightSum="1"> 

     <TextView 
      android:layout_width="276dp" 
      android:layout_height="wrap_content" 
      android:text="@string/warning_text" 
      android:id="@+id/main_boxText" 
      android:layout_margin="3dp" 
      android:textColor="@color/black" 
      android:textStyle="bold" 
      android:layout_weight="0.49" 
      android:textSize="12sp" 
      android:textIsSelectable="true" /> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:id="@+id/main_boxIcon" 
      android:src="@drawable/warning_icon" 
      android:layout_weight="0.47" /> 
    </LinearLayout> 

    <TableLayout 
     android:stretchColumns = "*" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:id="@+id/cellListTable"> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/blue" 
      android:paddingTop="@dimen/table_marginVertical" 
      android:paddingBottom="@dimen/table_marginVertical" 
      android:id="@+id/cellTableHeading"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:text="@string/cells" 
       android:id="@+id/cell" 
       android:layout_column="0" 
       android:textColor="@color/white" 
       android:textColorHighlight="@color/blue" 
       android:textSize="@dimen/table_textSize" 
       android:textIsSelectable="true" 
       android:paddingLeft="@dimen/table_marginVertical" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:text="@string/table_current_info" 
       android:id="@+id/currentInfo" 
       android:layout_column="1" 
       android:textColor="@color/white" 
       android:textColorHighlight="@color/blue" 
       android:textSize="@dimen/table_textSize" 
       android:textIsSelectable="true" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:text="@string/table_target_info" 
       android:id="@+id/targetInfo" 
       android:layout_column="2" 
       android:textColor="@color/white" 
       android:textColorHighlight="@color/blue" 
       android:textSize="@dimen/table_textSize" 
       android:textIsSelectable="true" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:text="@string/status" 
       android:id="@+id/status" 
       android:layout_column="3" 
       android:textColor="@color/white" 
       android:textColorHighlight="@color/blue" 
       android:textSize="@dimen/table_textSize" 
       android:textIsSelectable="true" 
       android:textAlignment="textEnd" 
       android:paddingRight="@dimen/table_marginVertical" /> 
     </TableRow> 
    </TableLayout> 
</LinearLayout> 

Ich habe keine Ahnung, warum es nicht zeigen. Ich habe versucht, aus dem Internet zu schauen, aber scheint, dass mein Problem nicht das gleiche ist. Ich habe keine Ausnahmen oder Fehler im Logcat. Ich habe versucht zu debuggen und herausgefunden, dass die Ansichten hinzugefügt werden, aber aus irgendeinem Grund werden sie nicht angezeigt. Hat jemand eine Idee dazu? Danke im Voraus.

UPDATE Ich habe das Layout Höhe der Standardansicht für alle wrap_content. Ich habe auch diese Codezeile hinzugefügt: vor dem Hinzufügen der Tabelle zu der tableLayout. Aber es funktioniert immer noch nicht.

tableRow.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT)); 
+0

Ich habe Ihren Code ausgeführt, es funktioniert gut überprüfen Sie für den Code, den Sie veröffentlicht haben. Überprüfen Sie Ihren Aufruf beim Erstellen, wenn Sie auf die korrekte Tabellenansichts-ID in findByViewIdMethod() hinweisen. –

+0

@jyotisakhare Danke, dass Sie mich darüber informiert haben. Habe immer noch keine Ahnung, denn es sollte die eine sein, wenn das TableLayout nicht das eine ist, sollte es eine Exception werfen, weil ich nur ein TableLayout in meinem Layout habe. –

Antwort

0

müssen Sie für jede Tabellenzeile ein Layout definieren und die textViews und ImageView an die entsprechenden Elemente im Layout binden.