Android Development Learning Path-Custom ListView (Inherit Baseadapter)

Source: Internet
Author: User

Junior students one, like programming, like Google, like Android, so the direction of choice is Android application development, open Bo First, hope that there will be more progress.

Recently, when I was doing an accounting app, I needed an activity to show the proportion of each item that was consumed each month, and the activity used a listview,listview to include a textview to display the name of the type. A textview to show the proportions, a ProgressBar to show the progress bar, so that the proportion of each item is clearer. such as (this only provides the implementation method, the interface effect is temporarily not provided)

Because this effect is more special, we need to implement the custom effect ourselves. Below to implement it:

First, define the layout of each item, Tendency_list_item.xml

<?XML version= "1.0" encoding= "Utf-8"?><Framelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:descendantfocusability= "Beforedescendants"Android:paddingleft= "5DP"Android:paddingright= "5DP" >    <RelativelayoutAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:descendantfocusability= "Afterdescendants" >        <TextViewAndroid:id= "@+id/tendency_title"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_marginbottom= "5DP"Android:layout_marginleft= "10DP"Android:layout_margintop= "15DP"android:textsize= "15SP" />        <ProgressBarAndroid:id= "@+id/tendency_progressbar"style= "? Android:attr/progressbarstylehorizontal"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_below= "@id/tendency_title"Android:layout_marginbottom= "15DP"Android:layout_marginleft= "10DP"Android:layout_marginright= "10DP"Android:max= "+"android:progressdrawable= "@drawable/progress_style" />        <TextViewAndroid:id= "@+id/tendency_total"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentright= "true"Android:layout_marginright= "10DP"Android:layout_margintop= "15DP"android:textsize= "15SP" />    </Relativelayout></Framelayout>

Complex redundancy can be ignored, is simple two textview and a ProgressBar just

Second, create a class, inherit Baseadapter, can be a separate class, can also be the current activity in the creation of an internal class, rewrite some functions, where the layout is hard-coded, if it is an external class of encapsulation, can be passed through parameters

 Public classMyadapterextendsBaseadapter {PrivateContext context;//the current context        PrivateLayoutinflater Minflater;//load the layout of each item with Layoutinflater        Private DoubleTotal//related to the project, the amount of gross position is passed in, which is why the use of internal classesList<map<string, object>> Map;//Storing data sources         PublicMyadapter (context context, list<map<string, object>>map,DoubleTotal ) {             This. Context =context;  This. Minflater =Layoutinflater.from (context);  This. Total =Total ;  This. Map =map; }        //override this method to get the total number of item in the current ListView@Override Public intGetCount () {returnlistmap.size (); }                //rewrite this method to get the current position@Override PublicObject GetItem (intposition) {            returnposition; } @Override Public LongGetitemid (intposition) {            returnposition; }        //an auxiliary class         Public classViewholder {TextView title;            ProgressBar progress;        TextView sum; }        //one of the most important ways to rewrite it to map the data to the layout@Override PublicView GetView (intposition, view view, ViewGroup parent) {View= Minflater.inflate (R.layout.tendency_list_item,NULL);//load the layout of the itemViewholder VH =NewViewholder ();//Creating auxiliary ClassesVh.title = (TextView) View.findviewbyid (r.id.tendency_title);//get an object by IDVh.progress =(ProgressBar) view. Findviewbyid (R.id.tendency_progressbar); Vh.sum=(TextView) View.findviewbyid (r.id.tendency_total); Vh.title.setText (String) map.get (position). Get ("type"));//call each object to map the data in            DoublePart = (Double) map.get (position). Get ("Total"); String Part2= String.Format ("%.0f", part * 100/Total ); Vh.sum.setText (Part2+ "%");            Vh.progress.setProgress (Integer.parseint (part2)); returnview; }    }

Third, the call can

new Myadapter (tendency_activity.  this, Listmap, sum); Show.setadapter (adapter);

The principle is also relatively simple, in the instantiation of the time, the first call GetCount () to calculate how many item, and then each position call GetView () to do the mapping of the data (so performance is not very good, how to optimize the future to add).

If there are errors, please also guide the corrections, thanks!

Android Development Learning Path-Custom ListView (Inherit Baseadapter)

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.