The use of Android learning Layoutinflater to compress multiple TextView components into a single view control in Myadapter GetView () and display them in a ListView

Source: Internet
Author: User

1, in the layoutinflater usually have the following 2 kinds of initialization method, in active call is very easy.

   1,  Layoutinflater inflater=layoutinflater.from (context);   2, Layoutinflater inflater = (layoutinflater) context.getsystemservice      (Context.layout_inflater_service);             

  

2, create Myadapter class time in the GetView () method when using Layoutinflater, usually will mainactivity in, will the context context into, This can be initialized in the GetView () method using the method above. For GetView () parameters, check the API documentation is very abstract, by setting a breakpoint in the code in memory to see the parameters found, theparent is the ListView. Convertview is a listview

Cache (data obtained).

public view getView (int position, View Convertview, ViewGroup parent) {//TODO auto-generated method Stub Layoutinflater inflater=layoutinflater.from (context);//mainactivity.inflater;// Layoutinflater Inflater = (layoutinflater) context.getsystemservice//(Context.layout_inflater_service); View view=inflater.inflate (r.layout.item2, NULL); 
//here is the item2.xml passed into the View object, so you can use method, Setcontentview (R.layout.activity_main)
in the main function is also the load XML, which displays the contents of the//xml in the current activity. Because the activity of the main function inherits the view, it is actually a container of the view,
so the main function can use the Findviewbyid method directly, can only find the ID of the space in the Activity_main, so if you want to use in other classes Span style= "color: #ff0000;" The
control in >activity_main can be mainactivity Act is passed in as a member of a class, and then Act.findviewbyid ( );
TextView text= (TextView) View.findviewbyid (r.id.tv_name); Text.settext (Personlist.get (position). GetName ()); TextView text2= (TextView) View.findviewbyid (R.id.tv_money); Text2.settext (Personlist.get (position). Getmoney () + "") ;

TextView text0=new TextView (context);
Text0.settext ("Hello");
return view;
The return value here, if used after a View.findviewbyid control, if multiple controls are either returned to view or returned with controls unrelated to these controls such as Text0,
You cannot return one of these, or an exception will occur, but if you only Findviewbyid a control, you can return the control. }

  

The use of Android learning Layoutinflater to compress multiple TextView components into a single view control in Myadapter GetView () and display them in a ListView

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.