2012-03-31 10 views
1

Ich habe Probleme, meine TableLayout meine Zeilen anzeigen zu lassen.Android TableLayout zeigt keine Zeilen an

ich propagieren die Zeilen programmatisch über diese Methode:

private void buildStatesTable() { 
    Log.d(SelectStatesPanel.class.getCanonicalName(), "Entering Build States Table"); 
    ArrayList<String> states = new ArrayList<String>(); 
    Random rand = new Random(); 

    for(int i = 0; i < 10; i++){ 
     if(i < goodStates.length){ 
      states.add(goodStates[i]); 
     }else{ 
      states.add(badStates[rand.nextInt(badStates.length)]); 
     } 
    } 

    Collections.shuffle(states); 

    TableLayout tl = (TableLayout) findViewById(R.id.statesTable); 
    final int NUM_PER_ROW = 2; 

    for(int i = 0; i < (states.size()/NUM_PER_ROW); i++){ 
     TableRow tr = new TableRow(getContext()); 
     tr.setLayoutParams(new LayoutParams(
       LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 

     for(int j = 0; j < NUM_PER_ROW && (i*NUM_PER_ROW + j) < states.size(); j++){ 
      TextView lblState = new TextView(getContext()); 
      lblState.setText(states.get(i*NUM_PER_ROW + j)); 
      lblState.setTextColor(Color.BLACK); 
      lblState.setLayoutParams(new LayoutParams(
        LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT)); 
      lblState.setClickable(true); 
      lblState.setOnClickListener(this); 
      tr.addView(lblState); 

      //Log.d(SelectStatesPanel.class.getCanonicalName(), "Adding State: " + states.get(i*NUM_PER_ROW + j)); 
      Log.d(SelectStatesPanel.class.getCanonicalName(), "\t\t\t (" + i + ", " + j + ")"); 
     } 

     // Add the TableRow to the TableLayout 
     tl.addView(tr); 
     Log.d(SelectStatesPanel.class.getCanonicalName(), "Adding Row"); 
    } 
} 

Und das ist meine XML (Tabellenlayout an der Unterseite ist):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0"   
     android:layout_margin="5dip" 

     android:text="Good choice! There's hope for you yet." 
     android:textSize="16dip" 
     android:textStyle="bold" 
     android:gravity="center" 
     /> 

    <TextView 
     android:id="@+id/txtLex" 

     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0"   
     android:layout_margin="5dip" 

     android:text="Lexington" 
     android:textSize="14dip" 
     android:gravity="center" 
     /> 
    <TextView 
     android:id="@+id/txtTampa" 

     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0"   
     android:layout_margin="5dip" 

     android:text="Tampa" 
     android:textSize="14dip" 
     android:gravity="center" 
     /> 
    <TextView 
     android:id="@+id/txtSacramento" 

     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0"   
     android:layout_margin="5dip" 

     android:text="Sacramento" 
     android:textSize="14dip" 
     android:gravity="center" 
     /> 
    <TextView 
     android:id="@+id/txtHiltonhead" 

     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0"   
     android:layout_margin="5dip" 

     android:text="Hiltonhead" 
     android:textSize="14dip" 
     android:gravity="center" 
     /> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"   
     android:layout_margin="5dip" 

     android:gravity="center" 
     android:src="@drawable/usa" 
     /> 
    <TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"   
     android:layout_margin="5dip" 
     android:id="@+id/statesTable" 
     android:stretchColumns="0,1">   
    </TableLayout> 


</LinearLayout> 

Warum ist nicht mein Tisch Layout der Inhalt zeigen?

Antwort

0

Ich entdeckte das Problem auf eigene Faust.

Das Problem war:

 lblState.setLayoutParams(new LayoutParams(
       LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 

Angeben TableRow.LayoutParams das Problem behoben.

0

Ich kann nicht sehen, was speziell in Ihrem Code falsch läuft, aber hier ist eine ähnliche Umsetzung in meinem eigenen Code:

public class EconFragment extends Fragment { 


    private TableLayout questionContainer; 
    static int pos = 0; 
    private String[] titles = {"The first title ", "hallo1","hallo2", "hallo3", 
      "hallo4", "hallo5","hallo6", "hallo7","hallo8", "hallo9"}; 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     Log.d("Econ", "onCreateView"); 

     return inflater.inflate(R.layout.econfragment, container, false); 
    } 

    public void onResume() { 
     super.onResume(); 


     LayoutInflater inflater = (LayoutInflater) getActivity(). 
     getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     questionContainer = (TableLayout) getActivity().findViewById(R.id.questionContainer); 



     //these lines are my first attempt to try and get the questions to repopulate after returning 
     //from pressing back - as of yet, they don't repopulate the screen: 
     //View econFragment = inflater.inflate(R.layout.econfragment, null); 
     //container.addView(econFragment); 

     int leftMargin=5; 
     int topMargin=5; 
     int rightMargin=5; 
     int bottomMargin=5; 
     while (pos < 10) { 
     View question = inflater.inflate(R.layout.question, null); 
     question.setId(pos); 
     TextView title = (TextView) question.findViewById(R.id.questionTextView); 
     title.setText(titles[pos]); 
     Button charts = (Button) question.findViewById(R.id.chartsButton); 
     charts.setId(pos); 
     charts.setOnClickListener(chartsListener); 
     TableRow tr = (TableRow) question; 
     TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
       TableLayout.LayoutParams.MATCH_PARENT, 
       TableLayout.LayoutParams.WRAP_CONTENT); 
     trParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin); 
     tr.setLayoutParams(trParams); 

     questionContainer.addView(tr); 
     pos++; 
     } 
     Log.d("Econ", "onResume"); 
    } 

Und die Quelle questionContainer, econfragment.xml:

<?xml version="1.0" encoding="utf-8"?> 

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/scrollView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <TableLayout 
      android:id="@+id/questionContainer" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

     </TableLayout> 
    </ScrollView> 

Wie Sie sehen, wird ein Question.class-Objekt als View-Objekt im EconFragment-Fragment erstellt. Das Frageobjekt wird verwendet, um ViewById für jeden benötigten Teil zu finden.

0

Kommentieren Sie den gesamten Zeilencode und ersetzen Sie ihn durch eine Zeile mit zwei Zeilen. Funktioniert es? Dann ist vielleicht das Array, aus dem Sie Ihre Zeilenansichten erstellen, das Problem. Mit freundlichen Grüßen.

+0

Das machte keinen Unterschied. Und Zeilen, die in der XML hinzugefügt wurden, werden angezeigt. Also habe ich keine Ahnung was los ist. – Malfist