Android ListView, detailed implementation steps, examples, custom ListView adapter

Source: Internet
Author: User

Android ListView, detailed implementation steps, examples, custom ListView adapter
    • 09. Four
    • /
    • Android Basics
    • /
    • No Comments

This article originates from www.ifyao.com Forbidden reprint! www.ifyao.com

How to use the ListView in Android is important because there are many times when the data in the database needs to be displayed in a list.

Specific Use steps: Gross

1. Create a Item.xml file that shows how each entry in the list is displayed in this layout file layout layout

2, add the ListView control in the interface that you want to use the ListView.

Register ListView in 3,activity

4, get the data, through the adapter to bind the data to the corresponding item, there are several kinds of adapters, the following detailed introduction

5,listview.setadapter (adapter);

Specific introduction

1, create a item.xml file

2, add the ListView control in the interface that you want to use the ListView.

Register ListView in 3,activity

4, get the data, bind the data to the corresponding item by the adapter

The example below uses the Simpleadapter adapter, which is the simplest of an adapter

The Show method in the example

Query the database first, get 20 data, the return type is List "person",

But Simpleadapter requirements data is the list "hashmap<string,?>" data type

So list<person> turned into list "hashmap<string,?>" type

The five parameters of the adapter are

1 Context object 2 data, that is, the top conversion of 31 start to define the item layout file resource ID 4, the data of each field 5, corresponding field to display to the specific control in item, meaning to bind the corresponding data to the corresponding control

5,listview.setadapter (adapter);

The second type of adapter requires that the incoming data be a cursor object

But one thing to note is that the primary key requirement in the query results is the _id field, and the two workarounds

1. Modify the primary key in the table to _id

2, use aliases when querying

Custom adapter, custom adapter inheritance Baseadapter

Methods to implement the parent class

Define the incoming data format yourself

Below is the general catalogue to be implemented

Get the layout service of the system in the constructor function

Focus on implementing the GetView method

First, because the ListView scrolls, it starts with a new view of the first screen, then caches the view and uses the front cached view directly after scrolling.

New view is a Layoutinflater-generated entry interface object through the layout manager

Start judging whether it is empty, empty is the first screen

Get the corresponding space to bind the data

Returns the interface object.

Calling a custom adapter, data binding is already bound in the adapter's method

The GetView method inside the adapter can also be optimized to improve performance

Each item in the ListView can start by hitting events properly bound data

by Onitemclicklistener

Click in the Event

Four parameters

1, is the ListView 2,itemview 3,item object in the ListView collection Ordinal 4 is not commonly used

Get the data bound by Getitematposition (serial number)

Android ListView, detailed implementation steps, examples, custom ListView adapter

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.