The Android ListView uses

Source: Internet
Author: User

Android's ListView

The ListView is one of the more common and complex controls in Android, which has both a default schema and a custom control that makes UI interaction more versatile and convenient.

The implementation process is divided into three steps:

    1. Set the ListView control in the activity layout and set the control ID to list;
    2. Create a new layout XML file in which to set the schema for each entry;
    3. Create a list of HashMap objects in your code, with the entry name key, to the contents of each entry;
    4. Creates a Simpleadapter object that creates a list object as a resource, a new layout as a parameter, and an array of strings, with parameters for an array of type int. You can set the adapter.

The code is as follows:

 /* * Set listview */protected void Onresume () {Super.onresume () in the Onresume method; list 

This is the simplest way to use the ListView, the ListView can also set more properties, the entry can have Button,imageview and other controls, as long as the settings adapter in the layout file to set these controls, Then write the IDs of these controls in the character array, and write the numbers in the int array in the R file.

A simple layout file

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="1dp"android:paddingBottom="1dp"><TextView android:id="@+id/mp3_name" android:layout_width="180dp" android:layout_height="30dp" android:textSize="20sp"/><TextView android:id="@+id/mp3_size" android:layout_width="180dp" android:layout_height="30dp" android:textSize="20sp"/></LinearLayout>

In the ListView, each entry can have a click Response event, click on an entry, similar to clicking on a button, for which you can set a click response event.

/** * ListView的点击响应事件,将点击的条目信息传递到新的播放界面的Activity中 */@Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Mp3Info mp3Info = infos.get(position); Log.i("Local", mp3Info.toString()); Intent playerintent = new Intent(); playerintent.putExtra("mp3Info", mp3Info); playerintent.setClass(LocalMp3ListActivity.this, PlayerActivity.class); startActivity(playerintent); }}

The Onlistitemclick function of the ListView is available, the parameter passed in the function has the control ID in the clicked entry, which control, and the order of the entry in the ListView. According to our needs to choose the right parameters to use, we can achieve the purpose of the response we want.

<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"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ListView android:id="@+id/android:list" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="vertical" android:drawSelectorOnTop="false"> </ListView> </LinearLayout></LinearLayout>

Layout that is set in the main interface.

The Android ListView uses

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.