android ListView單選功能

來源:互聯網
上載者:User

android ListView單選功能

在Adapter getView內部實現控制。


private int selectPosition =-1;class PayAdapter extends BaseAdapter{@Overridepublic int getCount() {// TODO Auto-generated method stubreturn payList==null?0:payList.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn payList.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic 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() {@Overridepublic 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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.