Adapter:
public class TestAdapter extends BaseAdapter {
private Context context;
private Listlist;
public TestAdapter(Context context,Listlist) {
// TODO Auto-generated constructor stub
context=this.context;
list=this.list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
// TODO Auto-generated method stub
ViewHold viewHold = null ;
if(view!=null){
view=LayoutInflater.from(context).inflate(R.layout.main_activity, null);
viewHold=new ViewHold();
viewHold.text=(TextView)view.findViewById(R.id.text);
view.setTag(viewHold);
}else {
view.getTag();
}
viewHold.text.setText(list.get(position));
return view;
}
private class ViewHold{
TextView text;
}
}
Activity:
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
super.setContentView(R.layout.testlist);
String a="abc";
Listlist=new ArrayList();
list.add(a);
ListView lv=(ListView)findViewById(R.id.test_list);
TestAdapter adapter=new TestAdapter(this,list);
lv.setAdapter(adapter);
}
}
調試發現context 為null mlist為null getcount是0 沒有調用getview
是哪裡出了問題?!
public TestAdapter(Context context,Listlist) {
// TODO Auto-generated constructor stub
context=this.context;//這句錯了
改成
public TestAdapter(Context context,Listlist) {
// TODO Auto-generated constructor stub
this.context=context;