listView_item中的点击事件


上篇写过一个listview单行展示数据时 new Textview来实现的,主要说的是 展示出来后一些界面设置问题,

下面 listView 加载 item ,并实现item中的 RadioButton 的点击事件,实现分数的加减


代码来也(重要代码):


加载item的代码:

	@SuppressLint("ResourceAsColor")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.list_item, parent, false);
			vh = new ViewHolder();
			vh.tvId = (TextView) convertView.findViewById(R.id.id);
			vh.tvName = (TextView) convertView.findViewById(R.id.name);
			vh.tvCredit = (TextView) convertView.findViewById(R.id.credit);
			vh.rbjia = (RadioButton) convertView.findViewById(R.id.rb_jia);
			vh.rbjian = (RadioButton) convertView.findViewById(R.id.rb_jian);
			
			convertView.setTag(vh);

		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		data.get(position);
		vh.tvId.setText((String) data.get(position).get("id"));
		vh.tvName.setText((String) data.get(position).get("name"));
		vh.tvCredit.setText((String) data.get(position).get("credit"));
		vh.rbjia.setOnCheckedChangeListener(new MyOnClick(data.get(position), true, position));//点击监听
		vh.rbjian.setOnCheckedChangeListener(new MyOnClick(data.get(position), false, position));//点击监听

		
		list[position] = false;
		

		return convertView;
	}
	class ViewHolder {
		private TextView tvId, tvName, tvCredit;
		private RadioButton rbjia, rbjian;
	}


RadioButton 点击开启:

	class MyOnClick implements OnCheckedChangeListener {
		
		private Map map;
		private Boolean isDui;
		private int pos;

		public MyOnClick(Map map, Boolean isDui, int pos) {
			this.map = map;
			this.isDui = isDui;
			this.pos = pos;
		}

		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			
				if (isChecked) {
					if (isDui) {
						num = num + Integer.parseInt((String) map.get("credit"));
						list[pos] = true;
					} else {
						if (list[pos]) {
							num = num - Integer.parseInt((String) map.get("credit"));
							list[pos] = false;
						}
					}

				}
			
			}

		}
就是这些,有需要的可以借鉴一下,大神勿喷,借过借过。

你可能感兴趣的:(listView_item中的点击事件)