Hi_Amos
坚持每天都在进步!!

如题,本文将介绍 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)

这里是要求返回来的结果集中主键必须为_id,这里我们设置的是personid,这里要么改写数据库结构,要么改写查询sql的语句.

如下所示:

   cursor=  db.rawQuery("select personid as _id,name,age from person",null);

 

本文源码:https://github.com/amosli/android_basic/tree/android_db

 

 

 

 

posted on 2014-06-17 01:34  Hi_Amos  阅读(6672)  评论(1编辑  收藏  举报