2012-04-23 3 views
0

Android 프로그래밍에 새롭습니다.Android tablelayout보기가 표시되지 않습니다.

테이블로 서식이 지정된 목록 (데이터베이스의 항목 포함)을 추가하려는 레이아웃 XML이 있습니다. 이 레이아웃에 아무것도 추가하지 않습니다

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.edit_cat); 

     DatabaseHandler db = new DatabaseHandler(this); 
     List<Category> allCategories = db.getAllCategories(); 

     //Get the main layout from XML 
     LinearLayout mainLayout = (LinearLayout) findViewById(R.id.edit_cat); 

     //Create table layout for the categories 
     TableLayout catList = new TableLayout(this); 

     //Iterate the categories, and organize in tables 
     for (Category category : allCategories) { 
      TableRow tr = new TableRow(this); 

      //Display category name 
      TextView name = new TextView(this); 
      name.setLayoutParams(new LayoutParams(      
        LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT)); 

      name.setText(category.getName()); 


      //Display category type 
      TextView type = new TextView(this); 
      type.setText(category.getType()); 
      type.setLayoutParams(new LayoutParams(
        LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT)); 

      //Display edit button 
      Button btnEdit = new Button(this); 
      btnEdit.setText(getResources().getString(R.string.edit)); 

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

      //Display delete button 
      Button btnDelete = new Button(this); 
      btnDelete.setText(getResources().getString(R.string.delete)); 

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


      tr.addView(name); 
      tr.addView(type); 
      tr.addView(btnEdit); 
      tr.addView(btnDelete); 

      catList.addView(tr, new TableLayout.LayoutParams(
        LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT)); 
     } 

     mainLayout.addView(catList); 


    } 

:

여기에 코드입니다. 이 아이디어가 효과가없는 이유는 무엇입니까?

편집 : 여기에 XML 코드를 추가 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/edit_cat" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_weight="1"> 

<TextView style="@style/pageHeader" 
    android:text="@string/catEditor"/> 

<Button 
    android:id="@+id/btnNewCategory" 
    android:text="@string/newCategory" 
    android:textSize="11pt" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:onClick="onBtnClicked"/> 
<Button 
    android:id="@+id/back" 
    android:text="@string/back" 
    android:textSize="11pt" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:onClick="onBtnClicked"/> 

</LinearLayout> 
+0

모두 선택하십시오. 범주가 비어 있지 않습니까? 데이터베이스에서 데이터를 가져 오는 중입니까? – kosa

+0

예, Log.v ('EditCat', category.getName())를 추가하고 문제없이 logCat에 목록을 가져 왔습니다. – LongInt

+0

edit_cat.xml 레이아웃 설정을 확인하는 것이 좋습니다. – kosa

답변

4

좋아, 난 당신에 대한 대답을 생각합니다.

LayoutParams를 지정할 때 좀 더 구체적이어야합니다. 컴파일 예외를 throw하지는 않지만 LayoutParams의 올바른 부모 클래스를 지정하지 않으면 아무 것도 표시되지 않습니다.

레이아웃 매개 변수를 지정할 때 객체가 배치되는 컨테이너의 클래스를 사용해야합니다. 당신이있는 LinearLayout 안에 진행되는 텍스트 뷰의 레이아웃 paramters을 설정하는 경우 당신은 TableRow 안에 갈 수있는 텍스트 뷰를 작성하는 경우, 당신은 new LinearLayout.LayoutParams(...) 이와 비슷하게 사용하는 것입니다, 당신은 아래

TableRow.LayoutParams(...) 사소한와 전체 코드 사용해야합니다 DB 설정이 없기 때문에 수정되었습니다.

LinearLayout mainLayout = (LinearLayout) findViewById(R.id.edit_cat); 

    //Create table layout for the categories   
    TableLayout catList = new TableLayout(this); 

    //Set the table layout to Match parent for both width and height 
    // Note: We use LinearLayout.LayoutParams because the TableLayout is going inside a LinearLayout 

    catList.setLayoutParams(
     new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 


    //Iterate the categories, and organize in tables  
    for(int i = 0; i < 5; i++) 
    { 
     TableRow tr = new TableRow(this); 
     /* Since the table row is going inside a table layout, we specify the parameters using TableLayout.LayoutParams */ 
     tr.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)); 

     //Display category name 
     TextView name = new TextView(this); 

     /* Since the TextView is going inside a TableRow, we use new TableRow.LayoutParams ... */ 
     name.setLayoutParams(new TableRow.LayoutParams(
       TableRow.LayoutParams.FILL_PARENT, 
       TableRow.LayoutParams.WRAP_CONTENT)); 

     name.setText("Test Row - " + i); 
     name.setBackgroundColor(Color.RED); 


     //Display category type 
     TextView type = new TextView(this); 
     type.setText("Type Row - " + i); 
     type.setBackgroundColor(Color.GREEN); 
     type.setLayoutParams(new TableRow.LayoutParams(
       TableRow.LayoutParams.FILL_PARENT, 
       TableRow.LayoutParams.WRAP_CONTENT)); 

     //Display edit button 
     Button btnEdit = new Button(this); 
     btnEdit.setText("Edit"); 

     btnEdit.setLayoutParams(new TableRow.LayoutParams(
       TableRow.LayoutParams.FILL_PARENT, 
       TableRow.LayoutParams.WRAP_CONTENT)); 

     //Display delete button 
     Button btnDelete = new Button(this); 
     btnDelete.setText("Delete"); 

     btnDelete.setLayoutParams(new TableRow.LayoutParams(
       TableRow.LayoutParams.FILL_PARENT, 
       TableRow.LayoutParams.WRAP_CONTENT)); 


     tr.addView(name); 
     tr.addView(type); 
     tr.addView(btnEdit); 
     tr.addView(btnDelete); 

     catList.addView(tr); 
    } 


    mainLayout.addView(catList); 
+0

감사하지만 작동하지 않았습니다 ... – LongInt

+0

눈치 챘을 지 모르지만이를 Tablelayout 대신 LinearLayout으로 변경했습니다. 또한 단지 제안 사항이지만 테이블 자체가 나타나지 않거나 삽입하는 모든 데이터인지 알기가 어렵습니다. XML 파일에 테이블을 임시로 생성하고 프로그램 적으로 행을 만드는 것이 좋습니다. 이것이 작동하면 프로그래밍 방식으로 테이블을 생성 할 수 있습니다. – Gophermofur

+0

좋아, 나는 그것을 시도 할 것이다. – LongInt

관련 문제