2 harry star Harry_Star 于 2015.05.26 17:59 提问

Java BaseAdapter的问题

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
是哪里出了问题?!

1个回答

danielinbiti
danielinbiti   Ds   Rxr 2015.05.26 18:03
已采纳
 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;
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!