[問題] 想問有關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