Add RaidoButton in ListView for android

Source: Internet
Author: User

Import android. app. Activity;

Import android. view. LayoutInflater;

Import android. view. View;

Import android. view. ViewGroup;

Import android. widget. BaseAdapter;

Import android. widget. CompoundButton;

Import android. widget. CompoundButton. OnCheckedChangeListener;

Import android. widget. ImageView;

Import android. widget. RadioButton;

Import android. widget. TextView;


Public class RecoverListAdapter extends BaseAdapter {


Private LayoutInflater inflater;


Int [] backup_record_item_image;


String [] backup_record_item_time;


String [] backup_record_item_to;


Activity activity;


Private int temp =-1;


Public RecoverListAdapter (Activity context, int [] backup_record_item_image,

String [] backup_record_item_time, String [] backup_record_item_to ){

This. inflater = LayoutInflater. from (context );

This. backup_record_item_image = backup_record_item_image;

This. backup_record_item_time = backup_record_item_time;

This. backup_record_item_to = backup_record_item_to;

This. activity = context;

}


@ Override

Public int getCount (){

Return backup_record_item_time.length;

}


@ Override

Public Object getItem (int position ){

Return position;

}


@ Override

Public long getItemId (int position ){

Return position;

}


@ Override

Public View getView (int position, View convertView, ViewGroup parent ){

ViewHolder holder;

If (convertView = null ){

Holder = new ViewHolder ();

ConvertView = this. inflater. inflate (R. layout. general_recover_list_item, null );

Holder. iv = (ImageView) convertView. findViewById (R. id. backup_record_item_image );

Holder. TV _time = (TextView) convertView. findViewById (R. id. backup_record_item_time );

Holder. TV _to = (TextView) convertView. findViewById (R. id. backup_record_item_to );

Holder. radioButton = (RadioButton) convertView

. FindViewById (R. id. backup_record_item_btn );

Holder. radioButton. setChecked (false );

ConvertView. setTag (holder );

} Else {

Holder = (ViewHolder) convertView. getTag ();

}


Holder. iv. setImageResource (backup_record_item_image [position]);

Holder. TV _time.setText (backup_record_item_time [position]);

Holder. TV _to.setText (backup_record_item_to [position]);


// The black part is the radio function part.


Holder. radioButton. setId (position );

Holder. radioButton. setOnCheckedChangeListener (new OnCheckedChangeListener (){

@ Override

Public void onCheckedChanged (CompoundButton buttonView, boolean isChecked ){

 


If (isChecked ){

If (temp! =-1 ){

RadioButton tempButton = (RadioButton) activity. findViewById (temp );

If (tempButton! = Null ){

TempButton. setChecked (false );

}

}

Temp = buttonView. getId ();

}

}

});


If (position = temp ){

Holder. radioButton. setChecked (true );

} Else {

Holder. radioButton. setChecked (false );

}

Return convertView;

}


Private class ViewHolder {

ImageView iv;


TextView TV _time;


TextView TV _to;


RadioButton radioButton;


}

}

 

General_recover_list_item.xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent">
<RelativeLayout
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: paddingLeft = "5dip"
Android: paddingRight = "5dip"
Android: id = "@ + id/backup_record_item">
<ImageView
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: src = "@ drawable/icon"
Android: layout_alignParentLeft = "true"
Android: id = "@ + id/backup_record_item_image"/>
 
<LinearLayout
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: orientation = "vertical"
Android: layout_toRightOf = "@ + id/backup_record_item_image"
Android: id = "@ + id/backup_record_item_txt">
<TextView
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: text = "time"
Android: layout_alignTop = "@ + id/imageView1"
Android: layout_alignBottom = "@ + id/imageView1"
Android: id = "@ + id/backup_record_item_time"/>
<TextView
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: text = "backup contact"
Android: layout_toRightOf = "@ + id/imageView1"
Android: layout_alignTop = "@ + id/imageView1"/>
<TextView
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: text = "mobile phone/memory card"
Android: layout_toRightOf = "@ + id/imageView1"
Android: layout_alignTop = "@ + id/imageView1"
Android: id = "@ + id/backup_record_item_to"/>
</LinearLayout>
<RadioButton
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: layout_alignParentRight = "true"
Android: id = "@ + id/backup_record_item_btn"/>


<CheckBox
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: visibility = "gone"
Android: text = ""
Android: id = "@ + id/backup_record_item_check"/>
</RelativeLayout>
</LinearLayout>


Main. xml
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical"
Android: id = "@ + id/recover_items">
<ListView
Android: id = "@ + id/recover_list"
Android: cacheColorHint = "#00000000"
Android: layout_height = "match_parent"
Android: layout_width = "fill_parent"/>
<View
Android: id = "@ + id/recover_footer"
Android: layout_width = "fill_parent"
Android: layout_height = "40dip"/>
</LinearLayout>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.