1 public class MainActivity extends Activity { 2 3 ListView listView = null; 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_main); 8 9 listView = (ListView) findViewById(R.id.base_list); 10 11 BaseAdapter baseAdapter = new BaseAdapter() { 12 @Override 13 public int getCount() { //返回多少,显示多少个列表项 14 return 10; 15 } 16 17 //该方法的返回值决定第position处列表项的内容 18 @Override 19 public Object getItem(int position) { 20 return position; 21 } 22 23 //该方法的返回值决定 第 i 处的列表项的ID 24 @Override 25 public long getItemId(int i) { 26 return i; 27 } 28 29 @Override 30 public View getView(int i, View view, ViewGroup viewGroup) { 31 // i 表示第几项<item>的id(从 0 开始) 32 // view 表示每个item的view 33 // viewGroup 表示父容器(例如:ListView,GridView,....) 34 35 36 //加载自定义的布局 37 view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.listview_item_style, null); 38 39 if (view != null) { 40 TextView textView = (TextView) view.findViewById(R.id.context); 41 textView.setText("我爱你中国!!!" + i); 42 textView.setTextSize(24); 43 44 ImageView imageView = (ImageView) view.findViewById(R.id.image); 45 imageView.setBackgroundResource(R.drawable.ic_launcher); 46 } 47 return view; 48 } 49 }; 50 51 //设置baseAdapter 52 listView.setAdapter(baseAdapter); 53 } 54 }