日期:2014-05-17  浏览次数:20685 次

android listview以html的网格显示

listview显示数据,类似HTML那样的table显示。显示不足的时候,横屏可以拖拉。


Activity 包括数据

package com.jdjw.test;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;
import com.jdjw.test.TableAdapter.TableCell;
import com.jdjw.test.TableAdapter.TableRow;
public class TestGridViewTableActivity extends Activity
{
 private ListView lvSaleNum;
 private List<TableRow> table;
 private Context mContext;
 @Override
 protected void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  setContentView(R.layout.test_gridview_table);
  lvSaleNum = (ListView)findViewById(R.id.lv_salesNum);
  mContext = this;
  data();
 }
 private void data(){  
  salesNum s1 = new salesNum("星期一",50,60,R.drawable.a0,60,80,R.drawable.a1);
  salesNum s2 = new salesNum("星期二",50,60,R.drawable.a0,10,80,R.drawable.a0);
  salesNum s3 = new salesNum("星期三",120,30,R.drawable.a1,30,80,R.drawable.a0);
  salesNum s4 = new salesNum("星期四",50,50,R.drawable.a0,80,80,R.drawable.a1);
  salesNum s5 = new salesNum("星期五",10,20,R.drawable.a0,62,60,R.drawable.a0);
  salesNum s6 = new salesNum("星期六",80,10,R.drawable.a1,90,80,R.drawable.a0);
  salesNum s7 = new salesNum("星期日",50,90,R.drawable.a0,50,30,R.drawable.a0);
  List<salesNum> data = new ArrayList<salesNum>();
  data.add(s1);
  data.add(s2);
  data.add(s3);
  data.add(s4);
  data.add(s5);
  data.add(s6);
  data.add(s7);
  
  
  int width = getWindowManager().getDefaultDisplay().getWidth()/7;
  table = new ArrayList<TableRow>();
  
  
  int height=30;//LayoutParams.FILL_PARENT;
  
  
  TableCell[] titleCells = new TableCell[7];
  List<TableCell> titleList = new ArrayList<TableCell>();
  
  TableCell title1 = new TableCell("时间",width + 8 ,height, TableCell.TEXT);//第一格
  titleList.add(title1);
  TableCell title2 = new TableCell("上月条码数",width + 8 ,height, TableCell.TEXT);//第一格
  titleList.add(title2);
  TableCell title3 = new TableCell("本月条码数",width + 8 ,height, TableCell.TEXT);//第一格
  titleList.add(title3);
  TableCell title4 = new TableCell("条码趋势",width + 8 ,height, TableCell.TEXT);//第一格
  titleList.add(title4);
  TableCell title5 = new TableCell("上月销售金额",width + 8 ,height, TableCell.TEXT);//第一格
  titleList.add(title5);
  TableCell title6 = new TableCell("本月销售金额",width + 8 ,height, TableCell.TEXT);//第一格
  titleList.add(title6);
  TableCell title7 = new TableCell("销售金额趋势",width + 8 ,height, TableCell.TEXT);//第一格
  titleList.add(title7);
  table.add(new TableRow(titleList.toArray(titleCells)));
  
  for(int i=0;i<data.size();i++){
   salesNum sales = data.get(i);
   TableCell[] cells = new TableCell[7];
   List<TableCell> cellsList = new ArrayList<TableCell>();
   TableCell cell1 = new TableCell(sales.getWeek(),width + 8 ,height, TableCell.TEXT);//第1格
   cellsList.add(cell1);
   TableCell cell2 = new TableCell(sales.getBackMonthNum(),width + 8 ,height, TableCell.TEXT);//第2格
   cellsList.add(cell2);
   TableCell cell3 = new TableCell(sales.getNextMonthNum(),width + 8 ,height, TableCell.TEXT);//第3格
   cellsList.add(cell3);
   TableCell cell4 = new TableCell(sales.getNumImage(),width + 8 ,height, TableCell.IMAGE);//第4格
   cellsList.add(cell4);
   TableCell cell5 = new TableCell(sales.getBackMonthMomey(),width + 8,height, TableCell.TEXT);
   cellsList.add(cell5);
   TableCell cell6 = new TableCell(sales.getNextMonthMomey(),width + 8 ,height, TableCell.TEXT);
   cellsList.add(cell6);
   TableCell cell7 = new TableCell(sales.getMomeyImage(),width + 8 ,height, TableCell.IMAGE);
   cellsList.add(cell7);
   table.add(new TableRow(cellsList.toArray(cells)));
  }

  //设置适配器
  TableAdapter adapter = new TableAdapter(mContext,table);
  lvSaleNum.setAdapter(adapter);
 }
 
 
 class salesNum{
  private String week;
  private int backMonthNum;
  private int nextMonthNum;
  private int numImage;
  private in