大家都知道如果要实作BaseAdapter的实作品都要改写getView函式;
可是我用起来就一直觉得很奇怪
因为我习惯用LinearLayout来排版
所以写起来就会像这样...
public class MyBaseAdapter2 extends BaseAdapter{
Activity activity;
ArrayList< ArrayList<String> > Data;
public View getView(int position,View convertView,ViewGroup parent) {
LinearLayout Line=new LinearLayout(activity);
Line.setOrientation(LinearLayout.HORIZONTAL);
int sz= Data.get(0).size();
TextView[] LineElement=new TextView[sz];
for (int i=0; i< sz; i++)
{
LineElement[i]=new TextView(activity);
LineElement[i].setText( Data.get(position).get(i));
Line.addView(LineElement[i]);
}
return Line;
}
}
上面那种写法跑起来可以跑
但是我想包在自己的class内处理
例如
public class MyStringModel {
Activity activity;
ArrayList<String> Data;
public LinearLayout Response;
MyStringModel(ArrayList<String> inn,Activity a) {
Data=inn; activity=a;
constructResponse();
}
private void constructResponse() {
Response=new LinearLayout(activity);
Response.setOrientation(LinearLayout.HORIZONTAL);
int sz= Data.get(0).size();
TextView[] LineElement=new TextView[sz];
for (int i=0; i<sz; i++) {
LineElement[i]=new TextView(activity);
LineElement.setText( Data.get(i));
Response.addView(LineElement[i]);
}
}
}
这样的话BaseAdapter2就可以这样写
public class BaseAdapter2 extends BaseAdapter {
MyStringModel[] M;
public BaseAdapter2(ArrayList< ArrayList<String> > data,Activity a) {
...
M=new MyStringModel[sz];
for (int i=0; i<sz; i++) {
M[i]=new MyStringModel(data.get(i),activity);
}
...
}
public View getView(int position,View convertView,ViwGroup parent) {
return M[position].Response;
}
}
可是怎么试都会当机....
有人能帮我看看吗?