) Rewrite the listview to implement the raidobutton option.

Source: Internet
Author: User

Transferred from: http://user.qzone.qq.com/334978366/infocenter!

The previous listview Implementation of radiobutton has a bug. When the content of the listview control exceeds the visible area of the screen, an error will be reported when sliding the listview control, the following comments are provided for errors and solutions. I will not talk about them much. Please refer to the source code for better solutions.

1. mainactivity. Java

Package com. excetop. listradio;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. util. log;
Import Android. View. view;
Import Android. View. viewgroup;
Import Android. widget. baseadapter;
Import Android. widget. Button;
Import Android. widget. compoundbutton;
Import Android. widget. listview;
Import Android. widget. radiobutton;
Import Android. widget. compoundbutton. oncheckedchangelistener;

Public class mainactivity extends activity {
Private Static final string tag = "mainactivity ";
Private listview;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Listview = (listview) This. findviewbyid (R. Id. List );
Myadapter adapter = new myadapter ();
Listview. setadapter (adapter );
}
Private class myadapter extends baseadapter {
Private string [] S = new string [] {"A", "B", "C", "D", "E", "A", "B ", "C", "D", "E", "A", "B", "C", "D", "E", "A", "B ", "C", "D", "E "};
Private int temp =-1;

@ Override
Public int getcount (){
// Todo auto-generated method stub
Return S. length;
}

@ Override
Public object getitem (INT position ){
// Todo auto-generated method stub
Return NULL;
}

@ Override
Public long getitemid (INT position ){
// Todo auto-generated method stub
Return 0;
}

@ Override
Public View getview (INT position, view convertview, viewgroup parent ){
Convertview = mainactivity. This. getlayoutinflater (). Inflate (R. layout. Item, null ); // Solution: Obtain the view again each time.
Button button = (button) convertview. findviewbyid (R. Id. Button );
Button. settext (s [position]);
Radiobutton = (radiobutton) convertview. findviewbyid (R. Id. radiobutton );
Radiobutton. setid (position); // set the position to the ID of radiobutton.
Radiobutton. setoncheckedchangelistener (New oncheckedchangelistener (){

@ Override
Public void oncheckedchanged (compoundbutton buttonview, Boolean ischecked ){

If (ischecked ){
// This sectionCodeTo implement the Single-choice function
If (temp! =-1 ){
Radiobutton tempbutton = (radiobutton) mainactivity. This. findviewbyid (temp );
If (tempbutton! = NULL ){
Tempbutton. setchecked (false );
}

}

Temp = buttonview. GETID ();
Log. I (TAG, "You are women--" + ischecked + "" + temp );

}
}
});

// The Single-choice ECHO is implemented here, which solves the unselected status of a Single-choice out of the screen range.
If (temp = position ){
Radiobutton. setchecked (true );
}
Return convertview;
}
// Holder;
// If (convertview = NULL) {// 1, Convertview is empty when the listview control is loaded for the first time.
// Convertview = mainactivity. This. getlayoutinflater (). Inflate (R. layout. Item, null ); // Execute this statement when the listview control does not slide
// Holder = new holder ();
// Convertview. settag (holder );
//} Else {
// Holder = (holder) convertview. gettag ();
//}
//
// Holder. Button = (button) convertview. findviewbyid (R. Id. Button );
// Holder. Button. settext (s [position]);
//
// Holder. radiobutton = (radiobutton) convertview. findviewbyid (R. Id. radiobutton );//
// Holder. radiobutton. setid (position ); // 2, because the radiobutton ID is reset here, convertview is not empty when sliding listview, and the above statement cannot get the radiobutton object, this statement will report a null pointer exception

// Holder. radiobutton. setoncheckedchangelistener (New oncheckedchangelistener (){
//
// @ Override
// Public void oncheckedchanged (compoundbutton buttonview, Boolean ischecked ){
//
// If (ischecked ){
// If (temp! =-1 ){
// Radiobutton tempbutton = (radiobutton) mainactivity. This. findviewbyid (temp );
// Tempbutton. setchecked (false );
//
//}
//
// Temp = buttonview. GETID ();
// Log. I (TAG, "You are women--" + ischecked + "" + temp );
//
//}
//}
//});
// Return convertview;
//}
// Private class holder {
// Private button;
// Private radiobutton;
//}
}
}

2, item. xml


xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">
Android: Id = "@ + ID/button"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "test">

Android: id = "@ + ID/radiobutton"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
/>

3, Main. XML


Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: TEXT = "@ string/Hello"
/>
Android: Id = "@ + ID/List"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
/>

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.