Three solutions for Android ListView Data Binding and display

Source: Internet
Author: User

First, create a layout for displaying an item named item. xml.

Copy codeThe Code is as follows: <? 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">

<TextView
Android: id = "@ + id/name"
Android: layout_width = "120dp"
Android: layout_height = "wrap_content"/>

<TextView
Android: id = "@ + id/phone"
Android: layout_width = "150dp"
Android: layout_height = "wrap_content"/>

<TextView
Android: id = "@ + id/amount"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>

</LinearLayout>

Then, in main. xml, add a ListView data display control and add the id as listview

The next step is to bind data. There are three methods in total:

Copy codeThe Code is as follows: private void Show1(){
List persons = ps. getScollData (0, 10 );
List <HashMap <String, Object> data = new ArrayList <HashMap <String, Object> ();
For (Person person: persons ){
HashMap <String, Object> item = new HashMap <String, Object> ();
Item. put ("amount", person. getAmount ());
Item. put ("id", person. getId ());
Item. put ("name", person. getName ());
Item. put ("phone", person. getPhone ());
Data. add (item );
}
SimpleAdapter adapter = new SimpleAdapter (getApplicationContext (), data, R. layout. item,
New String [] {"name", "phone", "amount"}, new int [] {R. id. name, R. id. phone, R. id. amount });
// Item indicates the previously defined item. xml, indicating that each object in the data set is bound to some items, that is, each item in the data is bound to a view item;
// The last two parameters indicate which keys are bound to which controls in the result set. (bind the object corresponding to key as name in the result set to the control with id as name in the view)
Listview. setAdapter (adapter); // the internal processing process is as follows:
// {Int total = adapter. getCount (); // The total number of Retrieved Data
// Int perpage = 7; // obtain the number of entries displayed on each page,
// For (int I = 0; I <perpage; I ++ ){
// View view = adapter. getView (I, convertView, parent); // the view of the previous execution is sent to convertView during the second execution;
// Display entries
//}}
}

Private voidShow2 (){// This method requires a Cursor in the result set and requires that the Cursor must have a field named _ id. Therefore, add a method to obtain the Cursor as follows!
Cursor cursor = ps. getCursorScollData (0, 10 );
SimpleCursorAdapter adapter = new SimpleCursorAdapter (this, R. layout. item, cursor,
New String [] {"name", "phone", "amount"}, new int [] {R. id. name, R. id. phone, R. id. amount });
Listview. setAdapter (adapter );
}

Public Cursor getCursorScollData (int offest, int maxResult ){
SQLiteDatabase db = dbOpenHelper. getReadableDatabase (); // The SQL statement is as follows because a word named _ id is required in the result set!
Cursor cursor = db. rawQuery ("select personid as _ id, name, phone, amount from person order by personid asc limit ?,? ",
New String [] {String. valueOf (offest), String. valueOf (maxResult )});
// Db. query (table, columns, selection, selectionArgs, groupBy, having, orderBy, limit );
Return cursor;
}

Private voidShow3 (){
List persons = ps. getScollData (0, 10); // For the PersonAdapter, see the custom adapter in the next section.
PersonAdapter adapter = new PersonAdapter (getApplicationContext (), persons, R. layout. item );
Listview. setAdapter (adapter );
}

You can add this method when you click an item to obtain information about this item:
Listview. setOnItemClickListener (new ItemClickListener ());

Private final class ItemClickListener implements OnItemClickListener {
@ Override // In this method, the first parameter is the display data (item) control. In this example, It is ListView. The third parameter is the position of the click item,
Public void onItemClick (AdapterView <?> Parent, View arg1, int position, long arg3 ){
ListView lview = (ListView) parent;
// Processing method corresponding to the show3 () method
// Person person Person = (Person) lview. getItemAtPosition (position );
// Toast. makeText (getApplicationContext (), person. getId (). toString (), 1). show ();

// Processing method corresponding to the show2 () method
// In the show2 method, the adapter returns Cursor,
// Cursor cursor = (Cursor) lview. getItemAtPosition (position );
// Int personid = cursor. getInt (cursor. getColumnIndex ("_ id "));
// Toast. makeText (getApplicationContext (), personid + "", 1). show ();

// Processing method corresponding to the show1 () method
// In the show1 method, the adapter returns a Map and then extracts the corresponding id value from the Map operation.
HashMap <String, Object> item = (HashMap <String, Object>) lview. getItemAtPosition (position );
Int personid = (Integer) item. get ("id ");
Toast. makeText (getApplicationContext (), personid + "", 1). show ();
}
}

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.