给listview循环出来的按钮添加事件

给listview循环出来的按钮添加事件

 

需要重写一个adapter类,继承BaseAdapter,里面的getView方法里面这样写:

 

@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		appInfo = listItem.get(position);
		AppItem appItem;
		if(convertView == null) {
			View v = LayoutInflater.from(mContext).inflate(R.layout.news_item, null);
			appItem = new AppItem();
			appItem.title = (Button)v.findViewById(R.id.news_title);
			
			v.setTag(appItem);
			convertView = v;
		} else {
			appItem = (AppItem)convertView.getTag();
		}
		appItem.title.setText(appInfo.get("title").toString()); 
		appItem.title.setOnClickListener(new OnClickListener() {
			    
			    @Override
			    public void onClick(View v) {
			    	Intent intent = new Intent(mContext, NewsDetailActivity.class);
					Map<String,Object> m =listItem.get(position);
					intent.putExtra("link", (String)m.get("link"));
					mContext.startActivity(intent);
			     
			    }
		});

		return convertView;
	}

 

你可能感兴趣的:(ListView)