android第十六步采用ListView实现数据列表显示

ListView绑定数据需要通过Adapter来绑定 listView.setAdapter(adapter);

Adapter分为三种

SimpleAdapter

参数:上下文对象、要绑定的数据、Item对象、字段名称、Item对象中对应的控件ID

SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, 
new String[]{"name","phone","amount"}, 
new int[]{R.id.name,R.id.phone,R.id.amount});

SimpleCursorAdapter:注意使用这个适配器游标对象中必须有一个字段为_id

参数:上下文对象、Item对象、游标对象、字段名称、Item对象中对应的控件ID

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});

自定义游标:必须继承BaseAdapter

LayoutInflater动态载入res/layout/下的xml布局文件,并且实例化与findViewById()类似不过它是用来找xml布局文件下的具体widget控件(如Button、TextView等)

WINDOW_SERVICEWindowManager管理打开的窗口程序

LAYOUT_INFLATER_SERVICELayoutInflater取得xml里定义的view
ACTIVITY_SERVICEActivityManager管理应用程序的系统状态
POWER_SERVICEPowerManger电源的服务
ALARM_SERVICEAlarmManager闹钟的服务
NOTIFICATION_SERVICENotificationManager状态栏的服务
KEYGUARD_SERVICEKeyguardManager键盘锁的服务
LOCATION_SERVICELocationManager位置的服务,如GPS
SEARCH_SERVICESearchManager搜索的服务
VEBRATOR_SERVICEVebrator手机震动的服务
CONNECTIVITY_SERVICEConnectivity网络连接的服务
WIFI_SERVICEWifiManagerWi-Fi服务
TELEPHONY_SERVICETeleponyManager电话服务
android第十六步采用ListView实现数据列表显示
public class PersonAdapter extends BaseAdapter {

    private List<Person> persons;
    private int elment;
    private LayoutInflater inflater;
    public PersonAdapter(Context context,List<Person> persons, int elment) {
        this.persons = persons;
        this.elment = elment;
        inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    }
    //条目
    @Override
    public int getCount() { 
        // TODO Auto-generated method stub
        return persons.size();
    }
    //根据位置返回数据
    @Override
    public Object getItem(int arg0) {
        
        return persons.get(arg0);
    }
    //返回选中ID
    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }
    
    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        TextView txtname;
        TextView txtphone;
        TextView txtamount;
        if(arg1 == null){
            arg1 = inflater.inflate(elment, null);
            ViewCache cache = new ViewCache();
            
            txtname = (TextView)arg1.findViewById(R.id.name);
               txtphone = (TextView)arg1.findViewById(R.id.phone);
               txtamount = (TextView)arg1.findViewById(R.id.amount);
               
            cache.txtname =txtname;
            cache.txtphone = txtphone;
            cache.txtamount = txtamount;
            
            arg1.setTag(cache); //放入缓存对象到TAG属性
        }
        else
        {
            ViewCache cache = (ViewCache)arg1.getTag();
            txtname = cache.txtname;
               txtphone = cache.txtphone;
               txtamount = cache.txtamount;
        }
        Person person = persons.get(arg0);
        txtname.setText(person.getName());
        txtphone.setText(person.getPhone());
        txtamount.setText(String.valueOf(person.getAmount()));
        return arg1;
    }
    //不是GET SET方法是因为会增加文件体积,会消耗更多内存
    private final class ViewCache{
        public  TextView txtname;
        public  TextView txtphone;
        public TextView txtamount;
    }
android第十六步采用ListView实现数据列表显示

 



android第十六步采用ListView实现数据列表显示,布布扣,bubuko.com

android第十六步采用ListView实现数据列表显示

上一篇:UI设计师不可不知的安卓屏幕知识


下一篇:微信公众平台开发入门