Android 用listview来做表格
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Android 用listview来做表格
ListView GridView ListView
GridView ListView
GridView ListView
GridView ListView
ListView(TextView)(ImageView)
XML()ListView HorizontalScrollView/ main.xml
Java
XML/HTML
1.
2. 3.android:orientation="vertical"android:layout_width="fill_parent" 4.android:layout_height="fill_parent"> 5. 6.android:layout_height="fill_parent"android:layout_width="fill_parent"> 7. 8.android:layout_width="wrap_content"> 9. 10. testMyListView.java java Java 1.packagetestMyListView; 2. 3.importjava.util.ArrayList; 4.importcom.testMyListView.TableAdapter.TableCell; 5.importcom.testMyListView.TableAdapter.TableRow; 6.importandroid.app.Activity; 7.importandroid.os.Bundle; 8.importandroid.view.View; 9.importandroid.widget.AdapterView; 10.importandroid.widget.ListView; youtParams; 12.importandroid.widget.Toast; 13. 14.publicclasstestMyListViewextendsActivity{ 15./**Calledwhentheactivityisfirstcreated.*/ 16.ListViewlv; 17.@Override 18.publicvoidonCreate(BundlesavedInstanceState){ 19.super.onCreate(savedInstanceState); 20.setContentView(yout.main); 21.this.setTitle("ListView---hellogv"); 22.lv=(ListView)this.findViewById(R.id.ListView01); 23.ArrayList 24.TableCell[]titles=newTableCell[5];//5 25.intwidth=this.getWindowManager().getDefaultDisplay().getWidth()/titles.length; 26.// 27.for(inti=0;i 28.titles[i]=newTableCell(""+String.valueOf(i), 29.width+8*i, youtParams.FILL_PARENT, 31.TableCell.STRING); 32.} 33.table.add(newTableRow(titles)); 34.// 35.TableCell[]cells=newTableCell[5];//5 36.for(inti=0;i 37.cells[i]=newTableCell("No."+String.valueOf(i), 38.titles[i].width, youtParams.FILL_PARENT, 40.TableCell.STRING); 41.} 42.cells[cells.length-1]=newTableCell(R.drawable.icon, 43.titles[cells.length-1].width, youtParams.WRAP_CONTENT, 45.TableCell.IMAGE); 46.// 47.for(inti=0;i<12;i++) 48.table.add(newTableRow(cells)); 49.TableAdaptertableAdapter=newTableAdapter(this,table); 50.lv.setAdapter(tableAdapter); 51.lv.setOnItemClickListener(newItemClickEvent()); 52.} 53.classItemClickEventimplementsAdapterView.OnItemClickListener{ 54.@Override 55.publicvoidonItemClick(AdapterView>arg0,Viewarg1,intarg2, 56.longarg3){ 57.Toast.makeText(testMyListView.this,""+String.valueOf(arg2)+"",500).show(); 58.} 59.} 60.} java Java 1.packagetestMyListView; 2. 3.importjava.util.List; 4.importandroid.content.Context; 5.importandroid.graphics.Color; 6.importandroid.view.Gravity; 7.importandroid.view.View; 8.importandroid.view.ViewGroup; 9.importandroid.widget.BaseAdapter; 10.importandroid.widget.ImageView; 11.importandroid.widget.LinearLayout; 12.importandroid.widget.TextView; 13. 14.publicclassTableAdapterextendsBaseAdapter{ 15.privateContextcontext; 16.privateList 17.publicTableAdapter(Contextcontext,List 18.this.context=context; 19.this.table=table; 20.} 21.@Override 22.publicintgetCount(){ 23.returntable.size(); 24.} 25.@Override 26.publiclonggetItemId(intposition){ 27.returnposition; 28.} 29.publicTableRowgetItem(intposition){ 30.returntable.get(position); 31.} 32.publicViewgetView(intposition,ViewconvertView,ViewGroupparent){ 33.TableRowtableRow=table.get(position); 34.returnnewTableRowView(this.context,tableRow); 35.} 36./** 37.*TableRowView 38.*@authorhellogv 39.*/ 40.classTableRowViewextendsLinearLayout{ 41.publicTableRowView(Contextcontext,TableRowtableRow){ 42.super(context); 43. 44.this.setOrientation(LinearLayout.HORIZONTAL); 45.for(inti=0;i 46.TableCelltableCell=tableRow.getCellValue(i); youtParamslayoutParams=youtParams( 48.tableCell.width,tableCell.height);// youtParams.setMargins(0,0,1,1);// 50.if(tableCell.type==TableCell.STRING){// 51.TextViewtextCell=newTextView(context); 52.textCell.setLines(1); 53.textCell.setGravity(Gravity.CENTER); 54.textCell.setBackgroundColor(Color.BLACK);// 55.textCell.setText(String.valueOf(tableCell.value)); 56.addView(textCell,layoutParams); 57.}elseif(tableCell.type==TableCell.IMAGE){// 58.ImageViewimgCell=newImageView(context); 59.imgCell.setBackgroundColor(Color.BLACK);// 60.imgCell.setImageResource((Integer)tableCell.value); 61.addView(imgCell,layoutParams); 62.} 63.} 64.this.setBackgroundColor(Color.WHITE);// 65.} 66.} 67./** 68.*TableRow 69.*@authorhellogv 70.*/ 71.staticpublicclassTableRow{ 72.privateTableCell[]cell; 73.publicTableRow(TableCell[]cell){ 74.this.cell=cell; 75.} 76.publicintgetSize(){ 77.returncell.length; 78.} 79.publicTableCellgetCellValue(intindex){ 80.if(index>=cell.length) 81.returnnull; 82.returncell[index]; 83.} 84.} 85./** 86.*TableCell 87.*@authorhellogv 88.*/ 89.staticpublicclassTableCell{ 90.staticpublicfinalintSTRING=0; 91.staticpublicfinalintIMAGE=1; 92.publicObjectvalue; 93.publicintwidth; 94.publicintheight; 95.privateinttype; 96.publicTableCell(Objectvalue,intwidth,intheight,inttype){ 97.this.value=value; 98.this.width=width; 99.this.height=height; 100.this.type=type; 101.} 102.}