Android 给ListView设置Adapter

Adapter:

class MyAdapter extends BaseAdapter {

        private List<Person> personList;



        public MyAdapter(List<Person> personList) {

            this.personList = personList;

        }



        @Override

        public int getCount() {

            return personList.size();

        }



        @Override

        public Object getItem(int position) {

            return null;

        }



        @Override

        public long getItemId(int position) {

            return 0;

        }



        /**

         * 使用打气筒

         */

        @Override

        public View getView(int position, View convertView, ViewGroup parent) {

            Person person  = personList.get(position);

            View view = View.inflate(MainActivity.this, R.layout.list_view, null);

            //在view里面查找

            TextView idTextView = (TextView) view.findViewById(R.id.id);

            //一定要是String类型

            idTextView.setText("ID:"+person.getId()+"");



            TextView nameTextView = (TextView) view.findViewById(R.id.name);

            nameTextView.setText(person.getName());



            TextView ageTextView = (TextView) view.findViewById(R.id.age);

            ageTextView.setText(person.getAge()+"");

            return view;

        }

 

你可能感兴趣的:(ListView)