2012-04-23 63 views
0

新的android编程。Android桌面布局视图不会出现

我有一个布局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

你确定allCategories不是空的吗?你从数据库中获取数据? – kosa 2012-04-23 19:12:48

+0

是的,我添加了Log.v('EditCat',category.getName())并在logCat中获得了没有问题的列表。 – LongInt 2012-04-23 19:16:39

+0

我会建议验证edit_cat.xml的布局设置alos。 – kosa 2012-04-23 19:18:53

回答

4

好吧,我想我已经为你找出答案。

当您指定LayoutParams时,您需要更具体。虽然它不会引发编译异常,但如果您没有指定LayoutParams的正确父类,则不会显示任何内容。

当您指定布局参数时,您需要使用该对象所在的容器的类。如果要设置这是一个的LinearLayout里面去一个TextView的布局paramters,你会用new LinearLayout.LayoutParams(...)相若方式,如果你正在创建一个TextView要走的TableRow里面,你需要使用TableRow.LayoutParams(...)

下面是完整的代码稍作因为我没有数据库设置。

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 2012-04-23 19:55:12

+0

不知道您是否注意到了,但我将其更改为LinearLayout而不是Tablelayout。此外,只是一个建议,但很难判断它本身没有显示出来,或者你插入的所有数据。我建议在XML文件(暂时)中创建表格,然后以编程方式创建行。当它工作时,您可以继续以编程方式创建表。 – Gophermofur 2012-04-23 20:00:05

+0

好吧,我会试试看。 – LongInt 2012-04-23 20:03:57