Android學習---ListView的點擊事件,simpleAdapter和arrayadapter,SimpleCursoAdapter的原理和使用

來源:互聯網
上載者:User

如題,本文將介紹 listview的點擊事件,simpleAdapter和arrayadapter的原理和使用. 1.ListView的註冊點擊事件複製代碼        //註冊點擊事件        personListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {            /**             *             * @param parent 當前ListView             * @param view 代表當前被點擊的條目             * @param position 當前條目的位置             * @param id 當前被點擊的條目的id             */            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                Log.d(tag,"點擊了!");                /*                方法 1:                 */                TextView tv_name = (TextView) view.findViewById(R.id.tv_name);                Toast.makeText(MyActivity.this,"姓名是1:"+tv_name.getText(),Toast.LENGTH_SHORT).show();                /*                方法 2:                 */                String name = persons.get(position).getName();                Toast.makeText(MyActivity.this,"姓名是2:"+name,Toast.LENGTH_SHORT).show();                /*                方法 3:                 */                Person p=(Person)parent.getItemAtPosition(position);                Toast.makeText(MyActivity.this,"姓名是3:"+p.getName(),Toast.LENGTH_SHORT).show();             }        });複製代碼注:註冊一個onItemClick事件,方法1:利用當前被點擊條目的視圖根據id找到tv_name,然後使用多士顯示被點擊的內容. 方法2:利用當前被點擊條目的位置,然後根據位置取到姓名; 方法3:parent在這裡表示ListView的位置,這裡得到被點擊條目的位置,取到person對象並顯示. com/amos/android_db/MyActivity.java 複製代碼package com.amos.android_db; import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.*;import com.amos.android_db.dao.Person;import com.amos.android_db.dao.PersonDao; import java.util.List; public class MyActivity extends Activity {     private ListView personListView;    private List<Person> persons;    LayoutInflater inflater;//打氣筒    String tag="MyActivity.class";     @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //inflater是一個系統服務,初始化系統服務,利用inflater將一個布局檔案轉化為一個對象        inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);         setContentView(R.layout.main);        PersonDao personDao = new PersonDao(this);        persons = personDao.findAll();          //第一步,得到組件的id的引用        personListView = (ListView) this.findViewById(R.id.listview_show_data);        //第二步,設定組件要顯示的內容,listview要顯示的內容比較複雜,需要資料的適配器        personListView.setAdapter(new MyListAdapter());         //註冊點擊事件        personListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {            /**             *             * @param parent 當前ListView             * @param view 代表當前被點擊的條目             * @param position 當前條目的位置             * @param id 當前被點擊的條目的id             */            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                Log.d(tag,"點擊了!");                /*                方法 1:                 */                TextView tv_name = (TextView) view.findViewById(R.id.tv_name);                Toast.makeText(MyActivity.this,"姓名是1:"+tv_name.getText(),Toast.LENGTH_SHORT).show();                /*                方法 2:                 */                String name = persons.get(position).getName();                Toast.makeText(MyActivity.this,"姓名是2:"+name,Toast.LENGTH_SHORT).show();                /*                方法 3:                 */                Person p=(Person)parent.getItemAtPosition(position);                Toast.makeText(MyActivity.this,"姓名是3:"+p.getName(),Toast.LENGTH_SHORT).show();             }        });     }     public class MyListAdapter extends BaseAdapter {         /**         * 返回當前有多少個條目         * @return         */         @Override        public int getCount() {            return persons.size();        }         /**         * 返回當前position位置對應的條目的object對象         * @param position         * @return         */        @Override        public Object getItem(int position) {            return persons.get(position);        }         /**         * 返回當前position位置對應條目的id         * @param position         * @return         */        @Override        public long getItemId(int position) {            return position;        }         /**         * 返回一個條目顯示的具體內容         * 計算當前介面 會有多少個條目出現         * 1.得到每一個textview的高度         * 2.得到listview的高度         * 3.listview高度/textview高度=得到了一個螢幕顯示textview的的個數         * listview的每一個條目的顯示都需要調用一次getView的方法         * 螢幕上有多個item顯示就會調用多少getview的方法         *         * @param position         * @param convertView         * @param parent         * @return         */         @Override        public View getView(int position, View convertView, ViewGroup parent) {//         TextView textView = new TextView(MyActivity.this);//         textView.setText(persons.get(position).getName()+":"+persons.get(position).getAge());//         return textView;             View view = inflater.inflate(R.layout.item, null);            Person person = persons.get(position);            TextView tv_name = (TextView) view.findViewById(R.id.tv_name);            TextView tv_age = (TextView) view.findViewById(R.id.tv_age);            tv_name.setText("姓名:" + person.getName());            tv_age.setText("年齡:" + person.getAge());            Log.d("item:",""+position);            return view;        }    } }複製代碼   2.simpleAdapter的使用複製代碼 //第二步,設定組件要顯示的內容,listview要顯示的內容比較複雜,需要資料的適配器 //      personListView.setAdapter(new MyListAdapter());//自實現BaseAdapter          List<Map<String, String>> data = new ArrayList<Map<String, String>>();        for (Person p : persons) {            Map<String, String> datavalue = new HashMap<String, String>();            datavalue.put("name", p.getName());            datavalue.put("age", p.getAge().toString());            data.add(datavalue);        }         personListView.setAdapter(new SimpleAdapter(this, data, R.layout.item, new String[]{"name", "age"}, new int[]{R.id.tv_name, R.id.tv_age}));複製代碼之前的適配器是繼承BaseAdapter,實現了getCount(),getItem(int position),getItemId(int position),getView(int position, View convertView, ViewGroup parent)方法,這裡調用SimpleAdapter方法,查看SimpleAdapter的實現也可以發現其也是繼承了BaseAdapter類. SimpleAdapter(android.content.Context context, java.util.List<? extends java.util.Map<java.lang.String, ?>> data, int resource, java.lang.String[] from, int[] to) { throw new RuntimeException("Stub!"); } context表示上下文,data表示要顯示的內容,resource表示資源檔的id,from這裡表示的是map裡的key,然後其id依次相對應為tv_name,和tv_age. 3.ArrayAdapter的使用複製代碼//3)arrayAdapter//      public  ArrayAdapter(android.content.Context context, int resource, int textViewResourceId, T[] objects) { throw new RuntimeException("Stub!"); }         String[] personArray = new String[persons.size()];        for(int i=0;i<persons.size();i++){            personArray[i] = persons.get(i).getName();        }        personListView.setAdapter(new ArrayAdapter(this,R.layout.item,R.id.tv_name,personArray));複製代碼這裡顯示的只是一側的內容,R.id.tv_name,這裡只顯示姓名,ArrayAdapter適合只有一列內容的資料顯示.       4.SimpleCursoAdapter的使用        //4)simpleCursorAdapter        //要求資料的組件名稱以底線        Cursor cursor = personDao.findAllByCursor() ;        personListView.setAdapter(new SimpleCursorAdapter(this,R.layout.item,cursor,new String[]{"name","age"},new int[]{R.id.tv_name,R.id.tv_age}));PersonDao.java中增加一個方法用來返回cursor結果集.   public Cursor findAllByCursor(){        SQLiteDatabase db = dbHelper.getReadableDatabase();        Cursor cursor=null;        if (db.isOpen()) {            cursor = db.query("person", null, null, null, null, null, null);        }運行時出現的問題: 複製代碼06-16 18:11:25.106      485-485/com.amos.android_db E/AndroidRuntime﹕ FATAL EXCEPTION: main    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.amos.android_db/com.amos.android_db.MyActivity}: java.lang.IllegalArgumentException: column '_id' does not exist            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)            at android.app.ActivityThread.access$1500(ActivityThread.java:117)            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)            at android.os.Handler.dispatchMessage(Handler.java:99)            at android.os.Looper.loop(Looper.java:123)            at android.app.ActivityThread.main(ActivityThread.java:3683)            at java.lang.reflect.Method.invokeNative(Native Method)            at java.lang.reflect.Method.invoke(Method.java:507)            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)            at dalvik.system.NativeStart.main(Native Method)     Caused by: java.lang.IllegalArgumentException: column '_id' does not exist            at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)            at android.widget.CursorAdapter.init(CursorAdapter.java:111)            at android.widget.CursorAdapter.<init>(CursorAdapter.java:90)            at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:47)            at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:84)            at com.amos.android_db.MyActivity.onCreate(MyActivity.java:69)            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)            at android.app.ActivityThread.access$1500(ActivityThread.java:117)            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)            at android.os.Handler.dispatchMessage(Handler.java:99)            at android.os.Looper.loop(Looper.java:123)            at android.app.ActivityThread.main(ActivityThread.java:3683)            at java.lang.reflect.Method.invokeNative(Native Method)            at java.lang.reflect.Method.invoke(Method.java:507)            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)            at dalvik.system.NativeStart.main(Native Method)複製代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.