程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ase dapter-Java BaseAdapter的問題

ase dapter-Java BaseAdapter的問題

編輯:編程綜合問答
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
是哪裡出了問題?!

最佳回答:


 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;
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved