码迷,mamicode.com
首页 > 移动开发 > 详细

Android:ListView(一)

时间:2015-08-27 00:28:28      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:android   listview   adapter   

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;
        }
    }

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

Android:ListView(一)

标签:android   listview   adapter   

原文地址:http://blog.csdn.net/qq_17326933/article/details/48015251

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!