我想画一个表格,用到了listview,参考的例子是这里:http://blog.csdn.net/hellogv/archive/2010/12/14/6075014.aspx
有两个文件:一个是Mylistview.java和TableAdapter.java,但是出现了错误(红色字体),麻烦大家帮下忙!!谢谢!!public class Mylistview extends Activity {
/** Called when the activity is first created. */
ListView listview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setTitle("课程表");
listview=(ListView)findViewById(R.id.listview1);
ArrayList<TableRow> table=new ArrayList<TableRow>();
TableCell[] title=new TableCell[6];
int width=this.getWindowManager().getDefaultDisplay().getWidth()/title.length;
title[0]=new TableCell("时间",width*8,LayoutParams.FILL_PARENT);
title[1]=new TableCell("星期一",width*8,LayoutParams.FILL_PARENT);
title[2]=new TableCell("星期二",width*8,LayoutParams.FILL_PARENT);
title[3]=new TableCell("星期三",width*8,LayoutParams.FILL_PARENT);
title[4]=new TableCell("星期四",width*8,LayoutParams.FILL_PARENT);
title[5]=new TableCell("星期五",width*8,LayoutParams.FILL_PARENT);
table.add(new TableRow(title));
TableCell cell1[]=new TableCell[6];
cell1[0]=new TableCell("第一二节",width*8,LayoutParams.FILL_PARENT);
cell1[3]=new TableCell("大学物理(第1-19周) 芦明霞 二教阶梯 D307",width*8,LayoutParams.FILL_PARENT);
cell1[4]=new TableCell("大学英语(第1-18周)单周 杨莉 一教小教A308",width*8,LayoutParams.FILL_PARENT);
table.add(new TableRow(cell1)); TableAdapter tableAdapter=new TableAdapter(this,table); listview.setAdapter(tableAdapter);
}
}
public class TableAdapter extends BaseAdapter{
private Context context;
private List<TableRow> table;
public TableAdapter(Context context,List<TableRow> table)
{
this.context=context;
this.table=table;
} public int getCount()
{
return table.size();
}
public long getItemId(int position)
{
return position;
}
public TableRow getItem(int position)
{
return table.get(position);
}
public View getView(int position,View convertView,ViewGroup parent)
{
TableRow tableRow=table.get(position);
return new TableRowView(this.context,tableRow);
}
class TableRowView extends LinearLayout{
public TableRowView(Context context,TableRow tableRow)
{
super(context);
this.setOrientation(LinearLayout.HORIZONTAL);
for(int i=0;i<tableRow.getSize();i++)
{
TableCell tableCell=tableRow.getCellValue(i);
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(tableCell.width,tableCell.height);
layoutParams.setMargins(0,0,1,1);
TextView textCell=new TextView(context);
textCell.setLines(1);
textCell.setGravity(Gravity.CENTER);
textCell.setBackgroundColor(Color.WHITE);
textCell.setText(String.valueOf(tableCell.value));
addView(textCell,layoutParams);
}
this.setBackgroundColor(Color.WHITE);
}
}
static public class TableRow{
private TableCell[] cell;
public TableRow(TableCell cell[]){
this.cell=cell;
}
public int getSize(){
return cell.length;
}
public TableCell getCellValue(int index){
if(index>=cell.length)
return null;
return cell[index];
}
} static public class TableCell{
public String value;
public int width;
public int height;
public TableCell(String value,int width,int height){
this.value=value;
this.width=width;
this.height=height;
} }
}
有两个文件:一个是Mylistview.java和TableAdapter.java,但是出现了错误(红色字体),麻烦大家帮下忙!!谢谢!!public class Mylistview extends Activity {
/** Called when the activity is first created. */
ListView listview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setTitle("课程表");
listview=(ListView)findViewById(R.id.listview1);
ArrayList<TableRow> table=new ArrayList<TableRow>();
TableCell[] title=new TableCell[6];
int width=this.getWindowManager().getDefaultDisplay().getWidth()/title.length;
title[0]=new TableCell("时间",width*8,LayoutParams.FILL_PARENT);
title[1]=new TableCell("星期一",width*8,LayoutParams.FILL_PARENT);
title[2]=new TableCell("星期二",width*8,LayoutParams.FILL_PARENT);
title[3]=new TableCell("星期三",width*8,LayoutParams.FILL_PARENT);
title[4]=new TableCell("星期四",width*8,LayoutParams.FILL_PARENT);
title[5]=new TableCell("星期五",width*8,LayoutParams.FILL_PARENT);
table.add(new TableRow(title));
TableCell cell1[]=new TableCell[6];
cell1[0]=new TableCell("第一二节",width*8,LayoutParams.FILL_PARENT);
cell1[3]=new TableCell("大学物理(第1-19周) 芦明霞 二教阶梯 D307",width*8,LayoutParams.FILL_PARENT);
cell1[4]=new TableCell("大学英语(第1-18周)单周 杨莉 一教小教A308",width*8,LayoutParams.FILL_PARENT);
table.add(new TableRow(cell1)); TableAdapter tableAdapter=new TableAdapter(this,table); listview.setAdapter(tableAdapter);
}
}
public class TableAdapter extends BaseAdapter{
private Context context;
private List<TableRow> table;
public TableAdapter(Context context,List<TableRow> table)
{
this.context=context;
this.table=table;
} public int getCount()
{
return table.size();
}
public long getItemId(int position)
{
return position;
}
public TableRow getItem(int position)
{
return table.get(position);
}
public View getView(int position,View convertView,ViewGroup parent)
{
TableRow tableRow=table.get(position);
return new TableRowView(this.context,tableRow);
}
class TableRowView extends LinearLayout{
public TableRowView(Context context,TableRow tableRow)
{
super(context);
this.setOrientation(LinearLayout.HORIZONTAL);
for(int i=0;i<tableRow.getSize();i++)
{
TableCell tableCell=tableRow.getCellValue(i);
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(tableCell.width,tableCell.height);
layoutParams.setMargins(0,0,1,1);
TextView textCell=new TextView(context);
textCell.setLines(1);
textCell.setGravity(Gravity.CENTER);
textCell.setBackgroundColor(Color.WHITE);
textCell.setText(String.valueOf(tableCell.value));
addView(textCell,layoutParams);
}
this.setBackgroundColor(Color.WHITE);
}
}
static public class TableRow{
private TableCell[] cell;
public TableRow(TableCell cell[]){
this.cell=cell;
}
public int getSize(){
return cell.length;
}
public TableCell getCellValue(int index){
if(index>=cell.length)
return null;
return cell[index];
}
} static public class TableCell{
public String value;
public int width;
public int height;
public TableCell(String value,int width,int height){
this.value=value;
this.width=width;
this.height=height;
} }
}
解决方案 »
- 关于main.xml的显示问题
- 关于Handler的问题
- Galaxy S4 模拟器无法启动
- libjpeg 编解码 YUV (图像处理)
- 请问android内存中的shared dirty memory,shared指的是什么啊
- 关于控件跨布局拖动的问题
- monkeyrunner不能同时操作两个设备的疑问
- android listview中的item的背景图将item的点击效果覆盖住了怎么办
- EditText,为什么我用的时候它就只是一条线那样的输入框呢
- 看看应该咋办?
- 请问高手,我对sdk源码执行make后为何只有system.img日期变了
- OAuth 认证的问题。我需要的那些源码的包在哪里导入呢?
"The constructor TableAdapter(Mylistview, ArrayList<TableRow>) is undefined“
说没构造函数,但是明明TableAdapter.java里面已经定义了!
TableRow 是系统自带的一个类 你自己又写了一个 构造函数参数是Context 而你用的table.add(new TableRow(title)); 不正确
希望对你有所帮助
import com.example.android.schedule.TableAdapter.TableRow;
怎么给分你,我没分..我到现在都不知道怎么获取分数,能用钱买吗?