Android:ListView(一)

ListView四种适配器:
1、ArrayAdapte
2、SimpleAdapter
3、SimpleCursorAdapter
4、BaseAdapter
下面是基于后面3中适配器实现的,还有一个就是SimpleAdapter的加强版,也就是plus,哈哈。主要是实现带图片功能,第一种适配器比较简单,准备用它实现显示分组。下面是实现功能截图以及代码。

这里写图片描述

这里写图片描述

上图实现的核心代码:

public class ListView1 extends Activity {
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview1);

         listView = (ListView) this.findViewById(R.id.listView1);
         showSimpleAdapter();
    }
    private void showSimpleAdapter() {
        List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
            for (int i = 0; i < 4; i++) {
                HashMap<String, Object> item = new HashMap<String, Object>();
                item.put("name","atm"+i );
                item.put("age",10+i );
                data.add(item);
            }

    /**
     * Context context  上下文信息
       List<? extends Map<String, ?>> data   将该数据显示在界面上
       int resource     数据需要显示的地方
       String[] from    从什么地方
       int[] to         显示到什么地方
         */
        SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,new String[]{"name", "age"}, new int[]{R.id.text1,R.id.text2});

        listView.setAdapter(adapter);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


}

这里写图片描述

上图实现的核心代码:

public class ListView2 extends Activity {
    private ListView listView;
    private ServiceTest serviceTest;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview1);

         listView = (ListView) this.findViewById(R.id.listView1);
         serviceTest=new ServiceTest(getApplicationContext());
         showSimpleCursorAdapter();
    }

    private void showSimpleCursorAdapter() {
        Cursor cursor =serviceTest.Find_cursor();
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor,
                new String[]{"name", "age"}, new int[]{R.id.text1, R.id.text2});
        listView.setAdapter(adapter);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

这里写图片描述

上图实现的核心代码:

public class ListView3 extends Activity {
        private ListView listView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.listview2);

             listView = (ListView) this.findViewById(R.id.listView2);
             showSimpleAdapter();
        }
        private void showSimpleAdapter() {
        SimpleAdapter adapter = new SimpleAdapter(
                this, getData(), R.layout.item_plus,new String[]{"name", "age", "img"}, new int[]{R.id.name,R.id.age,R.id.img});

            listView.setAdapter(adapter);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
        private List<Map<String, Object>> getData() {
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name", "atm1");
            map.put("age", 1);
            map.put("img", R.drawable.img2);
            list.add(map);

            map = new HashMap<String, Object>();
            map.put("name", "atm2");
            map.put("age", 2);
            map.put("img", R.drawable.love);
            list.add(map);

            map = new HashMap<String, Object>();
            map.put("name", "atm3");
            map.put("age", 3);
            map.put("img", R.drawable.img1);
            list.add(map);

            return list;
        }

    }

这里写图片描述

上图实现的核心代码:

public class ListView4 extends Activity {
        private ListView listView;
        private ServiceTest serviceTest;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.listview1);

             listView = (ListView) this.findViewById(R.id.listView1);
             serviceTest=new ServiceTest(this);
             listView.setOnItemClickListener(new ItemClickListener());
             showMyAdapter();
        }

        //点击后,获取给View上的数据
        private final class ItemClickListener implements OnItemClickListener{
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ListView listview = (ListView)parent;
                UserInfo userinfo = (UserInfo) listview.getItemAtPosition(position);
                Toast.makeText(getApplicationContext(), userinfo.getUser_id()+"  "+userinfo.getUser_name(), 1).show();
            }
        }
        private void showMyAdapter() {
            // TODO Auto-generated method stub
            List<UserInfo> list=serviceTest.Findall();
            MyAdapter  myAdapter=new MyAdapter(this, list, R.layout.item);
            listView.setAdapter(myAdapter);
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    }

代码下载请点击:代码下载

补充说明:在点击SimpleAdapter前,先通过源代码中的单元测试中的方法,添加数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值