zoukankan      html  css  js  c++  java
  • android_adapter

    package com.example.myfirstapp;
    
    import android.app.Activity;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ListView;
    import android.widget.SimpleCursorAdapter;
    import android.widget.Toast;
    
    public class PersonManagementActivity extends Activity {
        @Override
        protected void onCreate(Bundle state) {
            super.onCreate(state);
            setContentView(R.layout.activity_person_management);
            DBOpenHelper db=new DBOpenHelper(this);
            PersonService personService=new PersonService();
            personService.setDbOpenHelper(db);
    
            /*采用SimpleAdapter
            List<Person> personList=personService.queryPerson();
            List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();
            for(Person person:personList){
                Map<String,Object> map=new HashMap<String,Object>();
                map.put("_id", person.getId());
                map.put("name", person.getName());
                map.put("age", person.getAge());
                data.add(map);
            }
            SimpleAdapter adapter=new SimpleAdapter(this,data,R.layout.activity_person_listviewitem,
                                                    new String[]{"id","name","age"},
                                                    new int[]{R.id.id,R.id.name,R.id.age});
                                                    */
            
            Cursor c=personService.queryPersonCursor();
            //采用SimpleCursorAdapter 必须要有一个'_id'的字段
            SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.activity_person_listviewitem,c,
                                                                new String[]{"_id","name","age"},
                                                                new int[]{R.id.id,R.id.name,R.id.age});
            
            ListView listView=(ListView) findViewById(R.id.listView);
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    ListView lView=(ListView) parent;
    //                HashMap<String,Object> item=(HashMap<String, Object>) lView.getItemAtPosition(position);
    //                Toast.makeText(PersonManagementActivity.this, item.get("_id").toString(),1).show();
                    Cursor c=(Cursor) lView.getItemAtPosition(position);
                    Toast.makeText(PersonManagementActivity.this,c.getString(c.getColumnIndex("_id")),1).show();
                }
            });
            
        }
    }
  • 相关阅读:
    php记录代码执行时间
    java中针对同一变量的不同函数的互斥操作
    Linux下mysql新建账号及权限设置
    Linux下重启apache
    Mysql数据导入
    ubuntu安装phpcurl与phptidy扩展
    Linux服务器间文件传输
    Flash本地传递大数据,图片数据,localconnection 超出大小,超出限制 bitmapdata 拂晓风起
    [Java][JavaScript]字符串数组与字符串之间的互转(join/split)(转) 拂晓风起
    java poi读取excel公式,返回计算值(转) 拂晓风起
  • 原文地址:https://www.cnblogs.com/BigIdiot/p/2680249.html
Copyright © 2011-2022 走看看