zoukankan      html  css  js  c++  java
  • ListView用自定义适配器绑定数据

    1、在ListView里显示数据

    //自定义适配器
        private void show2() {
            List<Person> persons=service.getScrollData(0, 20);//需要绑定的数据,getScrollData代码在下方
    PersonAdapter adapter=new PersonAdapter(getApplicationContext(), persons, R.layout.item); listView.setAdapter(adapter); }

    2、getScrollData()代码

    /**
         * 分页获取记录
         * @param offset 跳过前面多少条记录
         * @param maxResult 每页显示多少条记录
         * @return
         */
        public List<Person> getScrollData(int offset,int maxResult){
            SQLiteDatabase db=dbOpenHelper.getReadableDatabase();
            List<Person> persons=new ArrayList<Person>();
            Cursor cursor=db.rawQuery("select * from person order by personid asc limit?,?", 
                    new String[]{String.valueOf(offset),String.valueOf(maxResult)});
            while(cursor.moveToNext()){
                int personid=cursor.getInt(cursor.getColumnIndex("personid"));
                String name=cursor.getString(cursor.getColumnIndex("name"));
                String phone=cursor.getString(cursor.getColumnIndex("phone"));
                int amount=cursor.getInt(cursor.getColumnIndex("amount"));
                persons.add(new Person(personid, name, phone,amount));
            }
            cursor.close();
            return persons;
        }

    3、一条数据单击事件

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            service=new PersonService(this);
            listView=(ListView)this.findViewById(R.id.listview);
            show();
            listView.setOnItemClickListener(new ItemClickListener());
            
        }
        
        private final class ItemClickListener implements OnItemClickListener{
    
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                ListView listView=(ListView)arg0;
                //自定义适配器
                Person person=(Person)listView.getItemAtPosition(arg2);
                Toast.makeText(getApplicationContext(), person.getName(), 2).show();
                        
            }
            
        } 
  • 相关阅读:
    CGI, FCGI, SCGI, WSGI 释异
    Boa Web Server 缺陷报告及其修正方法
    2.1 linux C 进程与多线程入门--(1)进程和程序的区别
    利用socket实现双机通信
    1、进程管理
    第一章:进程与线程总结
    8、linux网络编程--多播
    6、linux网络编程--UDP协议编程
    7、linux网络编程--广播
    4、linux网络编程--套接字的介绍
  • 原文地址:https://www.cnblogs.com/wdc224/p/3929606.html
Copyright © 2011-2022 走看看