标签:
listView的使用步骤:Sets the data behind this ListView. |
Returns the adapter currently in use in this ListView. |
ArrayAdapter
使用ArrayAdapter为listview填充数据的步骤:
1、在布局文件中添加ListView
2、在代码中初始化这个listview控件
3、调用listview.setAdapter()填充数据
//使用适配器为listview填充数据
//new ArrayAdapter:context 上下文,resourceId 条目布局文件的资源ID,object[] 要显示的数据
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.item, new String[]{"王菲","谢霆锋","张柏芝","李亚鹏"}));
SimpleAdapter
使用ArrayAdapter为listview填充数据的步骤:
1、在布局文件中添加ListView
2、在代码中初始化这个listview控件
3、调用listview.setAdapter()填充数据
//使用适配器为listview填充数据
//new SimpleAdapter:context 上下文,resourceId 条目布局文件的资源ID,String[] 要显示的列名,int[] 指定列显示在item布局文件的哪个控件上
lv.setAdapter(new SimpleAdapter(this, data, R.layout.item, new String[]{"id","name"}, new int[]{R.id.tv_id,R.id.tv_name}));
- 关联:条目的布局文件(这种简单的适配器只适用于这种布局)
GridView gv_home =(GridView)findViewById(R.id.gv_home);
gv_home.setAdapter(new MyAdapter());
gv_home.setOnItemClickListener(new OnItemClickListener() {
//点击item时调用这个方法
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (position) {
case 0://手机防盗
//检查之前是否设置了密码,如果设置了就弹出输入密码的对话框,
String password = sp.getString("password", "");
if(TextUtils.isEmpty(password)){
//显示设置密码的对话框
showSetupPasswordDialog();
}else{
//显示输入密码的对话框
showEnterPasswordDialog();
}
break;
case 8://设置中心
intent = new Intent(HomeActivity.this,SettingsActivity.class);
startActivity(intent);
break;
default:
break;
}
}
});
private class MyAdapter extends BaseAdapter{
@Override//注:九宫格的数目
public int getCount() {
return names.length;
}
@Override//注:每有一个条目(item)在屏幕上显示都会调用该方法
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if(convertView == null){
view = View.inflate(HomeActivity.this, R.layout.gv_home_item, null);
}else{
view = convertView;
}
ImageView iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
iv_icon.setImageResource(ids[position]);
tv_name.setText(names[position]);
return view;
}
@Override//注:得到指定位置的item
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override//得到指定位置item的id值
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
标签:
原文地址:http://www.cnblogs.com/candledragle/p/4249299.html