Android:ListView(一)

來源:互聯網
上載者:User

標籤: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(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.