写自己的MyAdapter实现BaseAdapter:
public class MyAdapter extends BaseAdapter { List<Map<String, Object>> list; LayoutInflater inflater; \\反射,和findviewByid差不多,但还是不同。 public MyAdapter(Context context){ inflater = LayoutInflater.from(context); } public List<Map<String, Object>> getList() { return list; } public void setList(List<Map<String, Object>> list) { this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = inflater.inflate(R.layout.item, null); \\通过反射找到item。 ImageView logo = (ImageView) view.findViewById(R.id.logo); TextView title = (TextView) view.findViewById(R.id.title); TextView version = (TextView) view.findViewById(R.id.version); TextView size = (TextView) view.findViewById(R.id.size); Map map = list.get(position); logo.setImageResource((Integer) map.get("logo")); title.setText((String) map.get("title")); version.setText((String) map.get("version")); size.setText((String) map.get("size")); return view; } }
在mainactivity中实现赋值给listview:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main2); lv_show = (ListView)findViewById(R.id.lv_show); //String[] data = {"你","是","一","个","坏", "人", "知", "道", "?"}; //ArrayAdapter<String> adapters = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data); ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("logo", R.drawable.camera); map.put("title", "美颜相机"); map.put("version", "1.0.1"); map.put("size", "21.3M"); list.add(map); map = new HashMap<String, Object>(); map.put("logo", R.drawable.music); map.put("title", "天天动听"); map.put("version", "2.1.1"); map.put("size", "23.1M"); list.add(map); map = new HashMap<String, Object>(); map.put("logo", R.drawable.movie); map.put("title", "暴风影音"); map.put("version", "3.1.0"); map.put("size", "20.1M"); list.add(map); map = new HashMap<String, Object>(); map.put("logo", R.drawable.google); map.put("title", "谷歌浏览"); map.put("version", "0.1.1"); map.put("size", "33.1M"); list.add(map); /*SimpleAdapter adapters = new SimpleAdapter( this, list, R.layout.item, new String[]{ "logo", "title", "version", "size" }, new int[] { R.id.logo, R.id.title, R.id.version, R.id.size } );*/ MyAdapter adapters = new MyAdapter(this); adapters.setList(list); lv_show.setAdapter(adapters);