首页 > 代码库 > 高级控件(三)

高级控件(三)

7.ListView事件接口
setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener) 选项选中时触发
setOnItemClickListener(AdapterView.OnItemClickListener listener) 选项单击时触发
setOnItemLongClickListener(AdaoterView.OnItemLongClickListener.listener) 选项长按时触发

lv1.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Map<String,Object> dateMap=(Map<String, Object>) adapter.getItem(position);
String value=http://www.mamicode.com/(String) dateMap.get("txt");
Log.i("aa", value);
}
});

8.ListActivity的使用
String [] date=getResources().getStringArray(R.array.city);

ArrayAdapter<String> ada=new ArrayAdapter<String>(
this
,android.R.layout.simple_list_item_multiple_choice
, date);
//设置adapter
setListAdapter(ada);
//设置ListVIew的选择行为:复选
ListView lv=getListView();
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

9.网格列表GridView
gv=(GridView) findViewById(R.id.gv);
List<Map<String, Object>> date=
new ArrayList<Map<String,Object>>();
//获得所有图片
Field[] fs=R.drawable.class.getDeclaredFields();
for(Field field:fs){
Map<String, Object>maps=new HashMap<String, Object>();
try {
int id=field.getInt(R.drawable.class);
maps.put("png", id);
date.add(maps);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
adapter=new SimpleAdapter(this,
date, R.layout.gridlist_item,
new String[]{"png"},
new int[]{R.id.iv99});
gv.setAdapter(adapter);

gv.setOnItemClickListener(new ItemClickImpl());
}
public class ItemClickImpl implements OnItemClickListener{

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
ImageView iv=new ImageView(GridViewImpl.this);
iv.setScaleType(ScaleType.CENTER);
iv.setLayoutParams(
new LinearLayout.LayoutParams
(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
Map<String, Integer> map=(Map<String, Integer>) adapter.getItem(position);
iv.setImageResource(map.get("png"));
Dialog d=new AlertDialog.Builder(GridViewImpl.this)
.setTitle("显示图片")
.setView(iv)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

}
})
.create();
d.show();
}

高级控件(三)