phosphorwqm 2012-01-06 19:16
浏览 393
已采纳

Android listview下面嵌套一个TableLayout问题?

UI的XML代码:main.xml

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

 <LinearLayout  

     android:id="@+id/LinearLayout01"  

     android:layout_width="fill_parent"  

     android:layout_height="fill_parent"  

     xmlns:android="http://schemas.android.com/apk/res/android">  

        

     <ListView android:layout_width="wrap_content"  

               android:layout_height="wrap_content"  

               android:id="@+id/MyListView">  

     </ListView>  

 </LinearLayout> 

   

 my_listitem.xml的代码如下,my_listitem.xml用于设计ListView的Item 

   

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

 <LinearLayout   

     android:layout_width="fill_parent"   

     xmlns:android="http://schemas.android.com/apk/res/android"   

     android:orientation="vertical"  

     android:layout_height="wrap_content"   

     android:id="@+id/MyListItem"   

     android:paddingBottom="3dip"   

     android:paddingLeft="10dip">   

     <TextView   

             android:layout_height="wrap_content"   

             android:layout_width="fill_parent"   

             android:id="@+id/ItemTitle"   

             android:textSize="30dip">   

     </TextView>   

     <TextView   

             android:layout_height="wrap_content"   

             android:layout_width="fill_parent"   

             android:id="@+id/ItemText">   

     </TextView>   

   

     <TableLayout 

         android:id="@+id/tl"

         android:layout_width="fill_parent"

         android:layout_height="wrap_content"

         android:stretchColumns="0,1"> 

     <TableRow> 

         <TextView 

             android:layout_height="wrap_content"

             android:text="a1"/> 

         <TextView 

             android:layout_height="wrap_content"

             android:text="a2"/> 

     </TableRow> 

     <TableRow> 

         <TextView 

             android:id="@+id/a1"

             android:layout_height="wrap_content"/> 

         <TextView 

             android:id="@+id/a2"

             android:layout_height="wrap_content"/> 

     </TableRow> 

    </TableLayout> 

 </LinearLayout>


如果没有TableLayout的情况下是这样的循环的:

 

 public void onCreate(Bundle savedInstanceState) {   

     super.onCreate(savedInstanceState);   

     setContentView(R.layout.main);   

     //绑定XML中的ListView,作为Item的容器   

     ListView list = (ListView) findViewById(R.id.MyListView);   

     ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();   

     for(int i=0;i<30;i++)  {   

         HashMap<String, String> map = new HashMap<String, String>();   

         map.put("ItemTitle", "This is Titl");   

         map.put("ItemText", "This is text");   

         mylist.add(map);   

     }   

     SimpleAdapter mSchedule = new SimpleAdapter(this,    

         mylist, 

         R.layout.my_listitem,        

         new String[] {"ItemTitle", "ItemText"},  

         new int[] {R.id.ItemTitle,R.id.ItemText});   

     list.setAdapter(mSchedule);   

 }

 

问题:这个listview下面的TableLayout的数据我是从数据库取出来循环显示的,那么我怎么写呢?

 

  • 写回答

6条回答 默认 最新

  • xxxxxxxxxxxxxxxxx 2012-01-06 20:52
    关注

    TableLayout container_table = (TableLayout )findViewById(R.id.tl);

    container_table.clearxxxxx;

    TabRow header = new TabRow(...)

    TextView column_1 = new TextView();
    column_1.setText("column_1");

    header.add(column_1);

    TextView column_2 = new TextView();
    column_2.setText("column_2");

    header.add(column_2);
    container_table.add(header);

    for(Item: List) {
    TabRow row = new TabRow();
    如上。。。。

    container_table.add(row);
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号