Use listview to display data

Source: Internet
Author: User

1. Define item first

Item. xml

 <?  XML version = "1.0" encoding = "UTF-8"  ?>  
< Linearlayout
Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: Orientation = "Horizontal"
Android: layout_width = "Fill_parent"
Android: layout_height = "Wrap_content" >
< Textview
Android: layout_width = "80dip"
Android: layout_height = "Wrap_content"
Android: ID = "@ + ID/personid"
/>
< Textview
Android: layout_width = "100dip"
Android: layout_height = "Wrap_content"
Android: ID = "@ + ID/name"
/>
< Textview
Android: layout_width = "Fill_parent"
Android: layout_height = "Wrap_content"
Android: ID = "@ + ID/amount"
/>
</ Linearlayout >

2. Bind the data to the listview through the adapter, and click a row of data to display the number

Method 1:

Method 1

 List  <  Hashmap <  String, Object  >  Data  =  New  Arraylist  <  Hashmap  <  String, Object  >  ();
For (Person: Persons ){
Hashmap < String, Object > Hashmap = New Hashmap < String, Object > ();
Hashmap. Put ( " ID " , Person. getpersonid ());
Hashmap. Put ( " Name " , Person. getname ());
Hashmap. Put ( " Amount " , Person. getamount ());
Data. Add (hashmap );
}

Simpleadapter Adapter = New Simpleadapter ( This , Data, R. layout. item, New String [] { " ID " , " Name " , " Amount " }, New Int [] {R. Id. personid, R. Id. Name, R. Id. Amount });
Listview. setadapter (adapter );
Listview. setonitemclicklistener ( New Onitemclicklistener (){
@ Override
Public Void Onitemclick (adapterview <?> Parent, view,
Int Position, Long ID ){
Listview mylistview = (Listview) parent;
Hashmap < String, Object > Item = (Hashmap < String, Object > ) Mylistview. getitematposition (position );
Toast. maketext (dbactivity. This , Item. Get ( " ID " ). Tostring (), Toast. length_short). Show ();
}

});

Method 2: Use a cursor

Method 2

 Cursor cursor  =  Personservice. getcursor (  0  ,  15  );
// The primary key must be _ ID in the field to design the table.
Simplecursoradapter cursoradapter = New Simplecursoradapter ( This , R. layout. Item, cursor, New String [] { " _ Id " , " Name " , " Amount " }, New Int [] {R. Id. personid, R. Id. Name, R. Id. Amount });
Listview. setadapter (cursoradapter );
Listview. setonitemclicklistener ( New Onitemclicklistener (){

/**
* Parent: The currently clicked listview object
* View the currently clicked entries
*/
@ Override
Public Void Onitemclick (adapterview <?> Parent, view,
Int Position, Long ID ){
Listview mylistview = (Listview) parent;
Cursor data = (Cursor) mylistview. getitematposition (position ); // Move cursor by location
Int M = Data. getint (data. getcolumnindex ( " _ Id " ));
Toast. maketext (dbactivity. This , String. valueof (M), Toast. length_short). Show ();
}

});

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.