@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// [1]找到我們關心的控件
ListView lv = (ListView) findViewById(R.id.lv);
//設置數(shù)據(jù)適配器
lv.setAdapter(new MyAdapter());
}
private class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return 7;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//[1]想辦法把我們自己定義的布局轉(zhuǎn)換成一個view對象 就可以了
View view;
if (convertView == null) {
//創(chuàng)建新的view對象 可以通過打氣筒把一個布局資源轉(zhuǎn)換成一個view對象
//resource 就是 我們定義的布局文件
//[一☆☆☆☆]獲取打氣筒服務
// view = View.inflate(getApplicationContext(), R.layout.item, null);
//[二☆☆☆☆]獲取打氣筒服務
// view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item, null);
//[三☆☆☆☆]獲取打氣筒服務
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.item, null);
}else {
//復用歷史緩存對象
view = convertView;
}
return view;
}
}
}