Use of the Android custom adapter combined with the Click event of the ListView

Source: Internet
Author: User

Below is a case of using a ListView and a custom adapter, clicking on an entry in the ListView will result in a dialog that changes the results, and immediately notifies the adapter to reload the data as follows:

(1), the layout file used to display the ListView:

<linearlayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  xmlns:tools  =" Http://schemas.android.com/tools " android:layout_width  = "match_parent"  android:layout_height  =" match_parent " android:orientation  =" vertical " android:padding  = "20DP" ;     <ListViewandroid:id="@+id/course_student_manage"android:layout_width ="Match_parent"android:layout_height="Match_parent" >                            </ListView></linearlayout>

The layout file used to display the information for each day is the item option, which needs to be used in the adapter

<?xml version= "1.0" encoding= "Utf-8"?><relativelayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  android:layout_width  =" fill_parent " android:layout_height  =" 80DP " android:padding  = "8DP" ;     <linearlayoutandroid:id="@+id/linearlayout1"android:layout_width= "Fill_parent" android:layout_height="80DP"android:orientation="vertical" >                                        <linearlayoutandroid:layout_width="Wrap_content"android:layout_ Height="40DP" >                                    <TextViewandroid:layout_width= "40dp"android:layout_height="40DP "android:text=" name "android:textsize=" 20sp " />                                                                            <Viewandroid:layout_width="Wrap_content"android:layout_height ="1DP"android:layout_weight="1" />                                                            <TextViewandroid:layout_width= "40dp"android:layout_height="40DP "android:text=" Course "android:textsize=" 20sp " />                                                                            <Viewandroid:layout_width="Wrap_content"android:layout_height ="1DP"android:layout_weight="1" />                                                            <TextViewandroid:layout_width= "40dp"android:layout_height="40DP "android:text=" fraction "android:textsize=" 20sp " />                                                                        </linearlayout>        <linearlayoutandroid:layout_width="Fill_parent"android:layout_ Height="40DP" >                                    <textview  android:id  = "@+id/tv_name"  android:layout_width= "80DP"  android:layout_height  = "50DP"  android:text  = ""  android:textsize  = "20sp" /> ;            <view  android:layout _width  = "wrap_content"  android:layout_he ight  = "1DP"  android:layout_weight  = "1" />             <textview  android:id  = "@+id/tv_course"  android:layout_width< /span>= "80DP"  android:layout_height  =
      
        "50DP" 
       android:text  =" " android:textsize  =" 20sp "/& gt;             <view  android:layout _width  = "wrap_content"  android:layout_he ight  = "1DP"  android:layout_weight  = "1" />             <TextViewandroid:id="@+id/tv_score"android:layout_width="40DP"  android:layout_height="40DP"android:text= ""android:textsize ="20sp" />                                                                                        </linearlayout>    </linearlayout></relativelayout>

The effect is as follows:

(3) Control of the ListView: Here are some ways to load the data, modify it according to the specific situation can be

 Public  class managestudentscoreactivity extends Activity  implements  Onitemclicklistener {            PrivateList<course> courselist;PrivateListView Course_student_manage;PrivateCourseadapter adapter;PrivateEditText Update_score;PrivateButton BTN_OK;PrivateString Studentname;PrivateString Coursename;PrivateAlertdialog Dialog;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.course_studen_manage); Course_student_manage = (ListView) This. Findviewbyid (R.id.course_student_manage);//Load DataCourselist = Loadcoursedata ();if(Courselist! =NULL) {adapter =NewCourseadapter ( This, courselist); }Else{Toastutil.showtoast ( This,"No student selection information!" ");        } course_student_manage.setadapter (adapter); Course_student_manage.setonitemclicklistener ( This); }/** * Check all student information * * @return  * *    protectedList<course>Loadcoursedata() {Courseinter Courseinter =NewCourseimpl ();returnCourseinter.getcoursedetail (); }/** * ListView Click event */     Public void Onitemclick(adapterview<?> adapter, view view,intPositionLongID) {//This is to create a popup dialog boxAlertdialog.builder Builder =NewAlertdialog.builder ( This);//Load Layout fileLayoutinflater Inflater = (layoutinflater) This. Getsystemservice (Context.layout_inflater_service); View Dialogview = inflater.inflate (R.layout.dialog_update_score,NULL);        Update_score = (EditText) Dialogview.findviewbyid (R.id.update_score);        BTN_OK = (Button) Dialogview.findviewbyid (R.ID.BTN_OK);        Builder.setview (Dialogview);        Builder.create (); Dialog = Builder.show ();//Be sure to show        //Get an entry for each item student name Course name and scoreView Adapterview = view;        TextView Tv_score = (TextView) Adapterview.findviewbyid (R.id.tv_score);        TextView tv_name = (TextView) Adapterview.findviewbyid (r.id.tv_name);        TextView Tv_course = (TextView) Adapterview.findviewbyid (r.id.tv_course);        Studentname = Tv_name.gettext (). toString ();        Coursename = Tv_course.gettext (). toString (); Btn_ok.setonclicklistener (NewOnclicklistener () { Public void OnClick(View v) {String scorestring = Update_score.gettext (). toString ();floatScore = Float.parsefloat (scorestring);//Change ResultsUpdatecoursedetail (Studentname, Coursename, score); }        });//Dialog.dismiss ();}/** * Update student results Information * * @param studentname * @param coursename * @param SC Ore * *    protected void Updatecoursedetail(String studentname, String coursename,floatScore) {Courseinter Courseinter =NewCourseimpl ();if(Courseinter.updatestudentcourse (Studentname, Coursename, score)) {Toastutil.showtoast (managestudentscoreactivity). This,"Modified successfully!" ");//Cancel Pop-up dialog boxDialog.dismiss ();//Notification adapter data has changedAdapter.notifydatasetchanged (); }Else{Toastutil.showtoast (managestudentscoreactivity). This,"The modification failed!" "); }    }}

(4) Customize the adapter as follows: In order to enable the adapter to automatically refresh, must inherit the method

@Override    publicvoidnotifyDataSetChanged() {        super.notifyDataSetChanged();    }

Specific adapter code:

 Public  class courseadapter extends baseadapter {    PrivateContext context;PrivateList<course> coursetlist; Public Courseadapter(context context, list<course> coursetlist) { This. Context = Context; This. coursetlist = coursetlist; } Public int GetCount() {returnCoursetlist.size (); } PublicObjectGetItem(intPosition) {returnCoursetlist.get (position); } Public Long Getitemid(intPosition) {returnPosition }/** * Notify adapter Update data */    @Override     Public void notifydatasetchanged() {Super. notifydatasetchanged (); } PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {if(Convertview = =NULL) {//The layout file for each item entry loaded hereConvertview = Layoutinflater.from (context). Inflate (R.layout.student_score_item,NULL);        } TextView Tv_name = (TextView) Convertview.findviewbyid (r.id.tv_name);        TextView Tv_course = (TextView) convertview. Findviewbyid (R.id.tv_course); TextView Tv_score = (TextView) Convertview.findviewbyid (R.id.tv_score);//Get a student of a locationCourse Course = coursetlist.get (position); Tv_name.settext (Course.getstudentname () +""); Tv_course.settext (Course.getcoursename () +""); Tv_score.settext (Course.getcoursesocre () +"");returnConvertview; }}

Ok! To complete this.

Use of the Android custom adapter combined with the Click event of the ListView

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.