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 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3