Android 0 Basics section 39th: listactivity and custom list items

Source: Internet
Author: User

Believe that through the first two period of study, and will develop the simplest list of the interface, then the current period to learn more techniques.

First, the use of listactivity

If the program's window simply needs to display a list, it can be implemented directly by the activity inheritance listactivity, listactivity subclasses do not need to call the Setcontentview () method to display an interface, Instead, you can directly pass in a subclass of content adapter,listactivity to render a list.

Next, you will learn a list based on the listactivity implementation through a simple sample program.

Continue to use the Listviewsample module of the Widgetsample project to create the Mylistactivity.java file under the Java package, with the following code:

 Packagecom.jinyu.cqkxzsxy.android.listviewsample;Importandroid.app.ListActivity;ImportAndroid.os.Bundle;ImportAndroid.widget.ArrayAdapter; Public classMylistactivityextendslistactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //define an arraystring[] persons = {"Crayon small new", "Pikachu", "Spider-Man", "Grey Wolf", "Black Cat Sheriff",                "Monkey King", "Ninja Turtle", "Mickey Mouse", "Hellokitty", "Cherry Balls"}; //wrapping an array into a arrayadapterarrayadapter<string> adapter =NewArrayadapter<string> ( This, Android.        R.layout.simple_list_item_single_choice, persons); //Set the window display listSetlistadapter (adapter); }}

There is only one ListView in the listactivity layout file, just set the adapter for listactivity.

Running the program, you can see the interface effect shown.

As you can see, the default layout of listactivity is made up of a list in the center of the screen.

Ii. Custom list items

Previous learning ListView is used by the Android system custom list item resources, basically are some plain text resources, the interface is not dazzling, there is no way to customize. In real-world development, lists often include icons, buttons, and other components, which require a developer to customize the list items to complete. The key is to provide enough data to the adapter adapter so that adapter can populate each row of the list with a richer view object.

The next step is to learn how to customize list items with an example.

Also use the Listviewsample module of the Widgetsample project to create a custom_item_layout.xml file in the app/main/res/layout/directory populated with the following code snippet:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <ListViewAndroid:id= "@+id/listview"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" /></LinearLayout>

Create a new Custom_item.xml list item layout file under the res/layout/directory with the following code:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal"android:gravity= "Center_vertical">    <ImageViewAndroid:id= "@+id/icon_img"Android:layout_width= "50DP"Android:layout_height= "50DP"android:padding= "5DP"android:src= "@mipmap/ic_launcher"/>    <TextViewAndroid:id= "@+id/content_tv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:textsize= "24SP"Android:textcolor= "#00f"/></LinearLayout>

This layout uses linearlayout as a row where the icon is on the left and the text is on the right.

Next, providing adapter,adapter for the ListView determines which list items the ListView will display. Create the Customitemactivity.java file under the Java package and load the new layout file above, with the following code:

 Packagecom.jinyu.cqkxzsxy.android.listviewsample;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View;ImportAndroid.widget.AdapterView;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListView;ImportAndroid.widget.Toast;Importjava.util.ArrayList;Importjava.util.List; Public classCustomitemactivityextendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.custom_item_layout); //Get Interface ListView componentListView ListView =(ListView) Findviewbyid (R.id.listview); //Define a list collection        Finallist<string> components =NewArraylist<>(); Components.add ("TextView"); Components.add ("EditText"); Components.add ("Button"); Components.add ("checkbox"); Components.add ("RadioButton"); Components.add ("ToggleButton"); Components.add ("ImageView"); //wrapping the list into Arrayadapterarrayadapter<string> adapter =NewArrayadapter<string> ( This, R.layout.custom_item, R.ID.CONTENT_TV, components); //set adapter for the ListViewListview.setadapter (adapter); //to bind a point to a ListView list item Click event ListenerListview.setonitemclicklistener (NewAdapterview.onitemclicklistener () {@Override Public voidOnitemclick (adapterview<?> adapterview, view view,intPositionLongID) {toast.maketext (customitemactivity. This, Components.get (position), Toast.length_short). Show ();    }        }); }}

The above code follows the overall structure of the ListView sample previously learned. The main difference is the use of the custom list layout r.layout.list_item. To create a arrayadapter, you must specify the following four parameters.

    • Context: To use a contextual environment, almost all components that are created need to be passed into the context object.

    • Resource: The custom list item layout resource ID to use.

    • Textviewresourceid: The ID of the TextView in the custom list layout, the TextView component will be the list item component of the Arrayadapter.

    • Objects: The array or list that you want to actually display will be responsible for providing data for multiple list items. The number of elements that the array or list contains, and how many list items will be generated.

Running the program, you can see the interface effect shown.

As you can see, the list layout uses our custom icons and also modifies the text display style.

But in this example, all the icons are the same, and often do not meet the actual development requirements, will be learned in the next section.

Come here today, if you have any questions welcome message to discuss together, also welcome to join the Android 0 Basic introductory Technical discussion group, grow together!

This article copyright for the public Share talent show (Shareexpert)--Xin 鱻 all, if necessary reprint please contact the author authorized, hereby declare!

Past period Summary share:

Android 0 Basics Introduction 1th: Android's past life

Android 0 Basics Section 2nd: Android system Architecture and application components those things

Android 0 Basics Section 3rd: Bring you up to talk about Android development environment

Android 0 Basics 4th: Installing and configuring the JDK correctly Ko fu the first trick

Android 0 Basics 5th: Use ADT bundles to easily meet the goddess

Android 0 Basics 6th: Configuration Optimization SDK Manager, official dating goddess

Android 0 Basics 7th: Take care of Android simulator and start the Sweet journey

Android 0 Basics 8th: HelloWorld, the starting point for my first trip

Android 0 Basics 9th: Android app, no code can be developed

Android 0 Basics Section 10th: Development IDE Big upgrade, finally ushered in Android Studio

Android 0 Basics Introductory Section 11th: Simple steps to take you to fly, run Android Studio project

Android 0 Basics 12th: Get familiar with the Android studio interface and start selling

Android 0 Basics 13th: Android Studio Configuration optimization to create a development tool

Android 0 Basics 14th: Using high-speed genymotion, stepping into the rocket era

Android 0 Basics Section 15th: Mastering the Android Studio project structure, sailing

Android 0 Basics Section 16th: Android User Interface Development overview

Android 0 Basics Section 17th: TextView Properties and Methods Daquan

Android 0 Basics Section 18th: EditText properties and how to use them

Android 0 Basics section 19th: Button usage explained

Android 0 Basics Section 20th: checkbox and RadioButton Usage Daquan

Android 0 Basics Section 21st: ToggleButton and switch usage Daquan

Android 0 Basics Section 22nd: ImageView's properties and methods Daquan

Android 0 Basics Section 23rd: ImageButton and Zoombutton use Daquan

Android 0 Basics Section 24th: Customize view simple to use to create your own controls

Android 0 Basics Section 25th: Simple and most commonly used linearlayout linear layouts

Android 0 Basics Section 26th: Two alignments, layout_gravity and gravity differ greatly

Android 0 Basics section 27th: Using padding and margin correctly

Android 0 Basics Section 28th: Easy to master relativelayout relative layout

Android 0 Basics 29th: Use tablelayout table layouts

Android 0 Basics 30th: Two minutes master Framelayout frame layout

Android 0 Basics Section 31st: absolutelayout absolute Layout with less

Android 0 Basics Section 32nd: New GridLayout Grid layout

Android 0 Basics section 33rd: Android Event Handling overview

Android 0 Basics Section 34th: Listening-based event handling in Android

Android 0 Basics Section 35th: Callback-based event handling in Android

Android 0 Basics Section 36th: Handling of Android system events

Android 0 Basics 37th: First Knowledge ListView

Android 0 Basics 38th: First Knowledge Adapter

Android 0 Basics section 39th: listactivity and custom list items

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.