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

    1、在ListView里显示数据

    private void show1() {
    		Cursor cursor=service.getCursorScrollData(0, 20);//需要绑定的数据,getCursorScrollData()代码在下方
    SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.item, cursor, new String[]{"name","phone","amount"}, new int[]{R.id.name,R.id.phone,R.id.amount}); listView.setAdapter(adapter); }

    2、getCursorScrollData()代码

        public Cursor getCursorScrollData(int offset,int maxResult){//使用SimpleCursorAdapter注意要包含_id字段
            SQLiteDatabase db=dbOpenHelper.getReadableDatabase();
            Cursor cursor=db.rawQuery("select personid as _id,name,phone,amount from person order by personid asc limit?,?", 
                    new String[]{String.valueOf(offset),String.valueOf(maxResult)});
            return cursor;
        }

    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;
                //SimpleCursorAdapter适配器
                Cursor cursor=(Cursor)listView.getItemAtPosition(arg2);
                String name=cursor.getString(cursor.getColumnIndex("name"));
                Toast.makeText(getApplicationContext(), name, 2).show();
                        
            }
            
        } 
  • 相关阅读:
    String类
    try catch异常捕获
    while循环语句
    编程中穷举法的运用
    for循环例题
    编程中的 if ()else() 语句
    代位符
    运算符_及_运算符优先级
    C#中的类型转换
    Asp.net基础知识
  • 原文地址:https://www.cnblogs.com/wdc224/p/3929599.html
Copyright © 2011-2022 走看看