Android learning notes (10) ---- detailed explanation of Android listview 1 (arrayadapter method)

Source: Internet
Author: User

/*************************************** **************************************** *************
* Author: conowen @ Dazhong
* E-mail: conowen@hotmail.com
* Http://blog.csdn.net/conowen
* Note: This article is original and only used for learning and communication. For more information, indicate the author and its source.
**************************************** **************************************** ************/

Simplecursoradapter method see http://blog.csdn.net/conowen/article/details/7306545


1. listview Overview

A view that shows items in a vertically scrolling list. The items come fromListAdapterAssociated with this view.

Simply put, create a listview and then give the value.

There are three numerical sources: arrayadapter, simpleadapter, and simplecursoradapter.

The first is the simplest adapter, which is a string value and can only display text information in listview.

The second type is a custom data source. to customize the layout, you can place images, buttons, text, and so on.

The third type of data comes from the database.

This article is the first method, arrayadapter, and the other two methods are similar, mainly because the adapter is different.

2. Use listview

Create the listview component, call the listview. arrayadapter () method, and set the adapter.

By callingsetOnItemClickListener() Interface method, set "click" a listener event of listview.

Call setonitemlongclicklistener() Interface method, set "Long press"A monitoring event of listview.


It should be noted that when the anonymous internal class new onitemclicklistener () is set, eclipse will not automatically load the rewrite function. Click the error message on the left and add unimplemented methods, to load the rewrite function onitemclick ().

mylistview.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {// TODO Auto-generated method stub}});

The onitemclick method is described in detail below. (The following is an official description. The parameter names are different and the types are the same. Arg0 = parent, arg1 = view, arg2 = position, arg3 = ID)

Public abstract void
Onitemclick (adapterview <?> Parent, view, int
Position, long ID)
Since:
API Level 1

Callback method to be invoked when an item in this adapterview has been clicked.

Implementers can call getitematposition (position) if they need to access the data associated with the selected item.

// When you click an item in listview, this callback method will be called.

Parameters
Parent The adapterview where the click happened.
View The view within the adapterview that was clicked (this will be a view provided by the adapter)
Position The position of the view in the adapter.
ID The row ID of the item that was clicked.

The last three parameters are described as follows,

View ------ is the content of one of the listview items you click. It comes from the adapter. For example, you can use (textview) arg1). gettext () to retrieve the content of the clicked item and convert it to the string type.

Position ---- is an item of the adapter. If you click the 2nd items of listview and the 2nd items correspond to the 2nd values of the adapter, the position value is 1.

For example, if it corresponds to the 3rd value of the adapter, then the position value is 2.

The value of ID ------- ID is the value corresponding to the item clicked in listview. If you click 2nd items in listview, the ID is equal to 1.

Note: These values start from 0.

/* Author: conowen * Date: 2012.2.26 */package COM. conowen. listview; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. adapterview; import android. widget. adapterview. onitemclicklistener; import android. widget. adapterview. onitemlongclicklistener; import android. widget. arrayadapter; import android. widget. listview; import android. widget. textview; import android. Widget. toast; public class listviewactivity extends activity {/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); listview Lv = (listview) findviewbyid (R. id. LV); string [] DATA = {"Windows", "Linux", "iOS", "android", "WP7", "Symbian"}; // defines adapterlv. setadapter (New arrayadapter <Str Ing> (this, // bind the adapter to Android in listview. r. layout. simple_expandable_list_item_1, data); // Click Event LV. setonitemclicklistener (New onitemclicklistener () {@ overridepublic void onitemclick (adapterview <?> Arg0, view arg1, int arg2, long arg3) {// todo auto-generated method stubtoast. maketext (listviewactivity. this, "What you click is item" + arg3 + ", toast. length_short ). show (); // retrieve the ID of the clicked item}); // long press event LV. setonitemlongclicklistener (New onitemlongclicklistener () {@ overridepublic Boolean onitemlongclick (adapterview <?> Arg0, view arg1, int arg2, long arg3) {// todo auto-generated method stubtoast. maketext (listviewactivity. this, (textview) arg1 ). gettext (), toast. length_long ). show (); // retrieve and click the content of a listview item return false ;}});}}

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.