[问题] 想问有关BaseAdapter的问题...

楼主: easterday (....)   2015-03-19 19:45:15
大家都知道如果要实作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;
}
}
可是怎么试都会当机....
有人能帮我看看吗?
作者: givemepass (λ)   2015-03-19 20:36:00
Log?
作者: TJLduck   2015-03-20 13:43:00
你这程式编译会过吗?LineElement.setText( Data.get(i));
作者: samsumg (松松)   2015-03-20 14:00:00
楼上这行编译可过 因为ArrayList<String>
作者: TJLduck   2015-03-20 14:20:00
LineElement[i].setText( Data.get(i)); 不是这样才会过
楼主: easterday (....)   2015-03-20 22:26:00
最后检查Log发现,如果把Horizontal和LayoutParam拿掉就不会当机了,但是这样很丑...

Links booklink

Contact Us: admin [ a t ] ucptt.com