Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android ListView單選功能

android ListView單選功能

編輯:關於Android編程

在Adapter getView內部實現控制。


private int selectPosition =-1;
	class PayAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return payList==null?0:payList.size();
		}

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

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

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if(convertView == null){
				convertView = LayoutInflater.from(context).inflate(R.layout.pay_listview_item_layout,
						null);
				holder = new ViewHolder();
				holder.image = (ImageView) convertView.findViewById(R.id.pay_bank_icon_imageview);
				holder.title = (TextView) convertView.findViewById(R.id.pay_bank_title_textview);
				holder.description = (TextView) convertView.findViewById(R.id.pay_bank_description_textview);
				holder.radio = (RadioButton) convertView.findViewById(R.id.pay_select_radiobtn);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			Pay pay = (Pay) getItem(position);
			holder.image.setImageResource(getResources().getIdentifier(pay.getImage(),
					"drawable", context.getPackageName()));
			holder.title.setText(pay.getTitle());
			holder.description.setText(pay.getDescription());
			holder.radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if(isChecked){
						selectPosition = position;
					} 
					notifyDataSetChanged();
				}
			});
			if(selectPosition==position){
				holder.radio.setChecked(true);
			}else{
				holder.radio.setChecked(false);
			}
			if(position==0){
				holder.radio.setChecked(true);
			}
			return convertView;
		}

	}

	class ViewHolder{
		ImageView image;
		TextView title;
		TextView description;
		RadioButton radio;
	}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved