Details about complex functions of Android ListView

Source: Internet
Author: User

 

------------------------------------------- Activity_main.xml
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">


<ListView
Android: id = "@ android: id/list"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"


>
</ListView>


</RelativeLayout> list_item.xml <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">


<TextView
Android: id = "@ + id/item_text"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_marginLeft = "10dp"
Android: textSize = "16sp"
Android: layout_weight = "1"
Android: textStyle = "bold"/>


<ImageView
Android: id = "@ + id/item_right"

Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>


<ImageView
Android: id = "@ + id/item_right1"

Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>


</LinearLayout>

 

 

 


------------------------------------------. Main
Package com. example. tezt;


Import java. util. ArrayList;
Import java. util. HashMap;
Import java. util. List;
Import java. util. Map;


Import android. app. ListActivity;
Import android. OS. Bundle;
Import android. view. Menu;


Public class MainActivity extends
ListActivity {


@ Override
Public void onCreate (
Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );


List <Map <String, Object> datas = getDatas ();
ImageAdapter adapter = new ImageAdapter (
This, datas );


This. getListView (). setAdapter (
Adapter );
}


Private List <Map <String, Object> getDatas (){
// TODO Auto-generated method stub
List <Map <String, Object> results = new ArrayList <Map <String, Object> ();
String [] items = this
. GetResources ()
. GetStringArray (
R. array. menu );


For (int I = 0; I <items. length; I ++ ){
Map <String, Object> item = new HashMap <String, Object> ();
Item. put ("right1 ",
R. drawable. icon2 );
Item. put ("text", items [I]);
Item. put ("right ",
R. drawable. icon1 );


Results. add (item );
}


Return results;


}


@ Override
Public boolean onCreateOptionsMenu (
Menu menu ){
GetMenuInflater (). inflate (
R. menu. activity_main,
Menu );
Return true;
}
}

 

 

 

-------------------------------------------. ImageAdapter


Package com. example. tezt;


Import java. util. List;
Import java. util. Map;


Import android. content. Context;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. BaseAdapter;
Import android. widget. ImageView;
Import android. widget. TextView;


Public class ImageAdapter extends
BaseAdapter {
Private List <Map <String, Object> datas;


Private Context context;


Public ImageAdapter (
Context context,
List <Map <String, Object> datas ){
This. context = context;
This. datas = datas;
}


@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return this. datas. size ();
}


@ Override
Public Object getItem (int position ){
// TODO Auto-generated method stub
Return this. datas. get (position );
}


@ Override
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return position;
}


@ Override
Public View getView (int position,
View convertView,
ViewGroup parent ){
// TODO Auto-generated method stub
ViewHolder holder = null;


If (convertView = null ){
Holder = new ViewHolder ();
ConvertView = LayoutInflater
. From (context)
. Inflate (
R. layout. list_item,
Null );
Holder. itemRightImage1 = (ImageView) convertView
. FindViewById (R. id. item_right1 );
Holder. itemText = (TextView) convertView
. FindViewById (R. id. item_text );
Holder. itemRightImage = (ImageView) convertView
. FindViewById (R. id. item_right );


ConvertView. setTag (holder );
}
Else {
Holder = (ViewHolder) convertView
. GetTag ();
}


Map <String, Object> item = this. datas
. Get (position );


Holder. itemRightImage1
. SetImageResource (Integer
. ParseInt (item
. Get ("right1 ")
. ToString ()));
Holder. itemText
. SetText (item. get (
"Text ")
. ToString ());
Holder. itemRightImage
. SetImageResource (Integer
. ParseInt (item
. Get ("right ")
. ToString ()));


Return convertView;
}


Static class ViewHolder {
ImageView itemRightImage1;
TextView itemText;
ImageView itemRightImage;
}


 

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.