ListView of Android Control

Source: Internet
Author: User

ListView is one of the most commonly used controls in Android. An important concept in ListView is Adapter, which serves as a bridge between controls and data sources.

Common data sources include arrays/sets (Array/List) and Cursor ).

1. obtain data from the array:

Running result:


2. obtain data from Cursor:
[Html]
// Get a cursor with all people
Cursor c = getContentResolver (). query (People. CONTENT_URI, null );
 
StartManagingCursor (c); // the life cycle of the used cursor varies with the Activity life cycle.
 
ListAdapter adapter = new SimpleCursorAdapter (this,
// Use a template that displays a text view
Android. R. layout. simple_list_item_1,
// Give the cursor to the list adatper
C,
// Map the NAME column in the people database...
New String [] {People. NAME },
// The "text1" view defined in the XML template
New int [] {android. R. id. text1 });
SetListAdapter (adapter );

Running result:


3. Use a custom Adapter

[Java]
Private class SpeechListAdapter extends BaseAdapter {
Public SpeechListAdapter (Context context ){
MContext = context;
}
 
Public int getCount (){
Return mTitles. length;
}
 
Public Object getItem (int position) {// Object corresponding to each entry
Return position;
}
 
Public long getItemId (int position) {// id of each entry
Return position;
}
 
Public View getView (int position, View convertView, ViewGroup parent) {// view corresponding to each item in ListView
SpeechView sv;
If (convertView = null ){
Sv = new SpeechView (mContext, mTitles [position],
MDialogue [position]);
} Else {
Sv = (SpeechView) convertView;
Sv. setTitle (mTitles [position]);
Sv. setDialogue (mDialogue [position]);
}
 
Return sv;
}
}
Result:


Some ListView Effects

1. ListView with delimiters.
The result is as follows (the red box indicates the separator ):

[Html]
Private class MyListAdapter extends BaseAdapter {
Public MyListAdapter (Context context ){
MContext = context;
}
 
Public int getCount (){
Return mStrings. length;
}
 
@ Override
Public boolean areAllItemsEnabled () {// whether all items can be clicked and selected
Return false;
}
 
@ Override
Public boolean isEnabled (int position) {// If the string starts with "-", the corresponding items are separated
Return! MStrings [position]. startsWith ("-");
}


 
Public Object getItem (int position ){
Return position;
}
 
Public long getItemId (int position ){
Return position;
}
 
Public View getView (int position, View convertView, ViewGroup parent ){
TextView TV;
If (convertView = null ){
TV = (TextView) LayoutInflater. from (mContext). inflate (
Android. R. layout. simple_expandable_list_item_1, parent, false );
} Else {
TV = (TextView) convertView;
}
TV. setText (mStrings [position]);
Return TV;
}
 
Private Context mContext;
}

2. ListView with shrinking and expanding Effects
Shows the effect:



3. Similar to ListView with floating letters (similar to the effect of Address Book navigation)
As shown in

 

The following describes the role of the yydatasetchanged () method:
NotifyDataSetChanged () every time an image is added, that is, when the data source changes, notifyDataSetChanged () is executed to refresh the interface.

 

ListView efficiency issues.
We know that the getView () method will be called for the display of an item, and the getView () method will be called without stopping sliding.
Next let's take a look at the effect of delayed Loading: "Loading" is used as the display text when the sliding is not stopped, and the actual text is displayed when the sliding is stopped.


Optimized the Adapter to improve the ListView execution efficiency.
[Java]
Public View getView (int position, View convertView, ViewGroup parent ){
// A ViewHolder keeps references to children views to avoid unneccessary CILS
// To findViewById () on each row.
ViewHolder holder;
 
// When convertView is not null, we can reuse it directly, there is no need
// To reinflate it. We only inflate a new View when the convertView supplied
// By ListView is null.
If (convertView = null) {// borrow the old view when there is an old item view. This avoids parsing xml every time.
ConvertView = mInflater. inflate (R. layout. list_item_icon_text, null );
 
// Creates a ViewHolder and store references to the two children views
// We want to bind data.
Holder = new ViewHolder ();
Holder. text = (TextView) convertView. findViewById (R. id. text );
Holder. icon = (ImageView) convertView. findViewById (R. id. icon );
 
ConvertView. setTag (holder );
} Else {
// Get the ViewHolder back to get fast access to the TextView
// And the ImageView.
Holder = (ViewHolder) convertView. getTag ();
}
 
// Bind the data efficiently with the holder.
Holder. text. setText (DATA [position]);
Holder. icon. setImageBitmap (position & 1) = 1? MIcon1: mIcon2 );
 
Return convertView;
}
 
Static class ViewHolder {
TextView text;
ImageView icon;
}
Author: johnny901114

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.