ListView的使用

1、ArrayAdapter -----   只可以与TextView绑定

A. extends ListActivity

    使用listView = getListView();时,ListView控件的id要设置为@android:id/list

B. 绑定数据适配器

    ArrayAdapter<String> data = new ArrayAdapter<String>(.this, R.layout.xxx);

   使用item的布局文件创建Adapter

C. listView.setAdapter(data);

    将Adapter输入ListView

D. listView.setOnItemClickListener(); //点击事件

     listView.setOnItemLongClickListener(); //长点击事件

2、SimpleAdapter -----  针对TextViewImageViewCheckBox

A. ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String,Object>>();

B. for (int i=0; i<count; i++) {

           HashMap<String, Object> hm = new HashMap<String, Object>;

           hm.put...

           Items.add(hm);

     }

C. SimpleAdapter(this, items, R.layout.xxx, String[] keys, int[] ids);

D. this.setAdapter(items);

3BaseAdapter -----   自定义Adapter

	lv = getListView();
		
	ArrayList <HashMap<String, Object>> al = new ArrayList <HashMap<String, Object>>();
	HashMap<String, Object> hm = new HashMap<String, Object>();
	hm.put("image", R.drawable.terminater);
	hm.put("name", "机器人");
	hm.put("owner", "DREAM");
	hm.put("score", "1星");
	hm.put("rating", "1");
	al.add(hm);
		
	hm = new HashMap<String, Object>();
	hm.put("image", R.drawable.calendar);
	hm.put("name", "DOTA");
	hm.put("owner", "DREAM");
	hm.put("score", "5星");
	hm.put("rating", "5");
	al.add(hm);
		
	hm = new HashMap<String, Object>();		
	hm.put("image", R.drawable.brick);
	hm.put("name", "WAR3");
	hm.put("owner", "DREAM");
	hm.put("score", "3星");
	hm.put("rating", "3");
	al.add(hm);
		
	MyAdapter1 ma1 = new MyAdapter1(this, al);
		
	lv.setAdapter(ma1);

	class MyAdapter1 extends BaseAdapter{
		private ArrayList <HashMap<String, Object>> mdata;
		private LayoutInflater mInflater;
    		private Context mcontext;
		
		public MyAdapter1(Context context, ArrayList<HashMap<String, Object>> data) {
			mdata = data;
			mcontext = context;
			mInflater = (LayoutInflater)mcontext.getSystemService(LAYOUT_INFLATER_SERVICE);			
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mdata.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mdata.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder h;
			if(convertView == null) {
				convertView = mInflater.inflate(R.layout.myadapterdemo1, null);
				h = new Holder();
				h.image = (ImageView)convertView.findViewById(R.id.image);
				h.name = (TextView)convertView.findViewById(R.id.name);
				h.owner = (TextView)convertView.findViewById(R.id.owner);
				h.rating = (RatingBar)convertView.findViewById(R.id.rating);
				h.score = (TextView)convertView.findViewById(R.id.score);
				
				convertView.setTag(h);
			} else {
				h = (Holder)convertView.getTag();
			}
			
			h.image.setBackgroundResource(Integer.parseInt(mdata.get(position).get("image").toString()));
			h.name.setText(mdata.get(position).get("name").toString());
			h.owner.setText(mdata.get(position).get("owner").toString());
			h.rating.setRating(Integer.parseInt(mdata.get(position).get("rating").toString()));
			h.score.setText(mdata.get(position).get("score").toString());
			
			return convertView;
		}
		
	}
	
	class Holder {
		ImageView image;
		TextView name;
		TextView owner;
		RatingBar rating;
		TextView score;
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值