Adapter, think of what to say (there will be an official version later)

Source: Internet
Author: User

Originally today is thinking to review the previous knowledge (to do a simple thing to do before), and then cheerfully prepared to summarize the use of adapter, but encountered the first and simplest of a arrayadapter has been hit and beaten.

This one may be more suitable for a rookie like me. Old driver can skip over! (above his business can see, maybe it will be useful)

1, member variable in the direct ListView lv_array= (ListView) Findviewbyid (R.id.lv_array) See clearly is in the member variable , this error I tune for two hours, the process will not say ... Compare DT, later thought carefully, and looked carefully to see only suddenly, in the implementation of Setcontentview (R.layout.activity_main), how can find the view of it?  Therefore, the order of the method call determines the Findviewbyid this method can not be used in advance! In other words, after Setcontentview (R.layout.activity_main)

ListView lv_array= (ListView) Findviewbyid (R.id.lv_array) or pre-defined Lv_array (declared at member variables) and then assigns a value lv_array= (listview) Findviewbyid (R.id.lv_array), as long as Findviewbyid () This method is not in front of the line.

2. Use arrayadapter<string> adp=new arrayadapter<> (this, r.layout.v_arrayadapter, str); A red tag refers to a file with the following code:

<?xml version= "1.0" encoding= "Utf-8"?><TextView     xmlns:android= "http://schemas.android.com/ Apk/res/android "    android:id=" @+id/tv_array "    android:layout_width=" Wrap_content "     android:layout_height= "Wrap_content"    android:textcolor= "#0f0"    android:text = "Wushuju"    />

Just a TextView component! Emphasize "only", my mistake is in this place, very silly X to write a textview in a linearlayout, the result program always crashes. (The original direct use of the simpleadapter, no requirement for this, so naturally wrong) error code is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout  xmlns:android= "http://schemas.android.com/apk/ Res/android "    android:layout_width=" Fill_parent "    android:layout_height=" Fill_parent "    ><TextView        android:id= "@+id/tv_array"    android:layout_width= "Wrap_content"     android:layout_height= "Wrap_content"    android:textcolor= "#0f0"     Android:text= "Wushuju"    /></linearlayout>

The lesson to me is, do not do, never know what the pit is waiting for you!

Talk about first here, continue to climb the pit!

Adapter, think of what to say (there will be an official version later)

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.