先上一下可以实现的效果图
要实现的效果有几方面
1、列不固定:可以根据数据源的不同生成不同的列数
2、表格内容可以根据数据源的定义合并列
3、要填写的单元格可以选择自定义键盘还是系统键盘
奔着这三点,做了个简单的实现,把源码贴一下(因为该点是主界面中的一部分,不便于放整个Demo)
自定义适配器,CallBackInterface是自定义的回调接口,这里定义回调是因为数据输入时需要及时保存
public class SiteDetailViewAdapter extends BaseAdapter implements CallBackInterface{
private Context context;
private LayoutInflater inflater;
private ArrayList> lists;
private KeyBoard keyBoard = null;//自定义键盘
private ListView listView = null;
private boolean isReadOnly = false;//是否是浏览状态
private String[] arrCellType = null;
private int[] arrHeadWidth = null;//每列宽度
public SiteDetailViewAdapter(Context context, ArrayList> lists
,KeyBoard keyBoard,ListView listView,boolean isReadOnly
,int[] arrHeadWidth) {
super();
this.context = context;
this.lists = lists;
inflater = LayoutInflater.from(context);
this.keyBoard = keyBoard;
this.listView = listView;
this.isReadOnly = isReadOnly;
this.arrHeadWidth = arrHeadWidth;
this.listView.setAdapter(this);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return lists.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int index, View view, ViewGroup arg2) {
HashMap map = lists.get(index);
String type = (String)map.get("rowtype");
ArrayList itemCells = new ArrayList();
//String cellValue,String cellKey,long cellType,int cellInRow,int cellSpan
ItemCell itemCellXuHao = new ItemCell((index+1)+"","-1",1,-1,1);
itemCells.add(itemCellXuHao);
for(int i=0;i
ItemCell itemCell = (ItemCell)map.get(i+"");
itemCells.add(itemCell);
}
//性能优化后需要放开注释danielinbiti
if(view == null||view!=null&&!((ListItemCustom)view.getTag()).getType().equals(type)){
view = inflater.inflate(R.layout.customel_list_item, null);
ListItemCustom itemCustom = (ListItemCustom)view.findViewById(R.id.custome_item);
itemCustom.buildItem(type, context, isReadOnly,arrHeadWidth,itemCells,index
,this.listView,this.keyBoard,this);