android - How to create a custom ListView with "extends Activity"? -
i learning android programming, able create custom listview with:
public class project1 extends listactivity { .... } with xml file custom layout. know how same thing with
public class project1 extends activity { .... } all tutorials looked @ use extends listactivity, know there make things simpler, sake of learning how hard way???
have @ tutorial. http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html
go thru post. advanced : lazy load of images in listview
edited according comment :
consider example of displaying list of countries , abbreviated names
first custom listview have create xml file representing listview :
<?xml version="1.0" encoding="utf-8"?> <!--main.xml--> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffccd0" > <textview android:text="countries list" android:textcolor="#b3000d" android:gravity="center_vertical|center_horizontal" android:textsize="26dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textstyle="bold" android:background="#ffb0b6" android:layout_marginbottom="5dip" android:typeface="sans"/> <relativelayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#570000"> <textview android:id="@+id/tv_1" android:textcolor="#ffffff" android:gravity="center_vertical|left" android:textsize="16dip" android:layout_height="wrap_content" android:textstyle="bold" android:typeface="serif" android:layout_width="70dip" android:paddingleft="20dip" android:text="abbr"> </textview> <textview android:id="@+id/tv_2" android:textcolor="#ffffff" android:gravity="center_vertical|left" android:textsize="16dip" android:layout_height="wrap_content" android:textstyle="bold" android:typeface="serif" android:layout_width="200dip" android:layout_torightof="@+id/tv_1" android:text="countries"> </textview> </relativelayout> <listview android:id="@+id/lv_country" android:layout_height="wrap_content" android:layout_width="fill_parent" android:cachecolorhint="#00000000"> </listview> </linearlayout> now have create xml file representing row of list item. row.xml file:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:gravity="left|center" android:layout_width="wrap_content" android:paddingbottom="10px" android:paddingtop="10px" android:paddingleft="3px"> <textview android:id="@+id/textview01" android:layout_width="70dip" android:scrollhorizontally="true" android:layout_height="wrap_content" android:gravity="left" android:layout_weight="1" android:singleline="true" android:textsize="15dip" android:textstyle="bold" android:textcolor="#d08021" android:paddingleft="20dip"> </textview> <textview android:id="@+id/textview02" android:layout_weight="2" android:layout_width="200dip" android:scrollhorizontally="true" android:layout_height="wrap_content" android:gravity="left" android:singleline="true" android:layout_marginleft="10dip" android:textsize="15dip" android:textstyle="bold" android:textcolor="#7f0000"> </textview> </linearlayout> as want display array of strings in listview, have used static array of strings , stored in countrieslist.java file.
this countrieslist.java file :
public class countrieslist { public static string[] countries = { "afghanistan", "albania albania albania albaniaalbaniaalbaniaalbania albania ", "algeria", "american samoa", "andorra", "angola", "anguilla", "antarctica", "antigua , barbuda", "argentina", "armenia", "aruba", "australia", "austria", "azerbaijan", "bahamas", "bahrain", "bangladesh", "barbados", "belarus", "belgium", "belize", "benin", "bermuda", "bhutan", "bolivia", "bosnia , herzegovina", "botswana", "bouvet island", "brazil", "british indian ocean territory", "brunei darussalam", "bulgaria", "burkina faso", "burundi", "cambodia", "cameroon", "canada", "cape verde", "cayman islands", "central african republic", "chad", "chile", "china", "christmas island", "cocos (keeling) islands", "colombia", "comoros", "congo", "congo, democratic republic of the", "cook islands", "costa rica", "cote d'ivoire", "croatia", "cuba", "cyprus", "czech republic", "denmark", "djibouti", "dominica", "dominican republic", "ecuador", "egypt", "el salvador", "equatorial guinea", "eritrea", "estonia", "ethiopia", "falkland islands (malvinas)", "faroe islands", "fiji", "finland", "france", "french guiana", "french polynesia", "french southern territories", "gabon", "gambia", "georgia", "germany", "ghana", "gibraltar", "greece", "greenland", "grenada", "guadeloupe", "guam", "guatemala", "guinea", "guinea-bissau", "guyana", "haiti", "heard island , mcdonald islands", "holy see (vatican city state)", "honduras", "hong kong", "hungary", "iceland", "india", "indonesia", "iran, islamic republic of", "iraq", "ireland", "israel", "italy", "jamaica", "japan", "jordan", "kazakhstan", "kenya", "kiribati", "korea, democratic people's republic of", "korea, republic of", "kuwait", "kyrgyzstan", "lao people's democratic republic", "latvia", "lebanon", "lesotho", "liberia", "libyan arab jamahiriya", "liechtenstein", "lithuania", "luxembourg", "macao", "macedonia, former yugoslav republic of", "madagascar", "malawi", "malaysia", "maldives", "mali", "malta", "marshall islands", "martinique", "mauritania", "mauritius", "mayotte", "mexico", "micronesia, federated states of", "moldova, republic of", "monaco", "mongolia", "montserrat", "morocco", "mozambique", "myanmar", "namibia", "nauru", "nepal", "netherlands", "netherlands antilles", "new caledonia", "new zealand", "nicaragua", "niger", "nigeria", "niue", "norfolk island", "northern mariana islands", "norway", "oman", "pakistan", "palau", "palestinian territory, occupied", "panama", "papua new guinea", "paraguay", "peru", "philippines", "pitcairn", "poland", "portugal", "puerto rico", "qatar", "reunion", "romania", "russian federation", "rwanda", "saint helena", "saint kitts , nevis", "saint lucia", "saint pierre , miquelon", "saint vincent , grenadines", "samoa", "san marino", "sao tome , principe", "saudi arabia", "senegal", "serbia , montenegro", "seychelles", "sierra leone", "singapore", "slovakia", "slovenia", "solomon islands", "somalia", "south africa", "south georgia , south sandwich islands", "spain", "sri lanka", "sudan", "suriname", "svalbard , jan mayen", "swaziland", "sweden", "switzerland", "syrian arab republic", "taiwan, province of china", "tajikistan", "tanzania, united republic of", "thailand", "timor-leste", "togo", "tokelau", "tonga", "trinidad , tobago", "tunisia", "turkey", "turkmenistan", "turks , caicos islands", "tuvalu", "uganda", "ukraine", "united arab emirates", "united kingdom", "united states", "united states minor outlying islands", "uruguay", "uzbekistan", "vanuatu", "venezuela", "viet nam", "virgin islands, british", "virgin islands, u.s.", "wallis , futuna", "western sahara", "yemen", "zambia", "zimbabwe" }; public static string[] abbreviations = { "af western sahara western saharawestern sahara western saharawestern sahara", "al", "dz", "as", "ad", "ao", "ai", "aq", "ag", "ar", "am", "aw", "au", "at", "az", "bs", "bh", "bd", "bb", "by", "be", "bz", "bj", "bm", "bt", "bo", "ba", "bw", "bv", "br", "io", "bn", "bg", "bf", "bi", "kh", "cm", "ca", "cv", "ky", "cf", "td", "cl", "cn", "cx", "cc", "co", "km", "cg", "cd", "ck", "cr", "ci", "hr", "cu", "cy", "cz", "dk", "dj", "dm", "do", "ec", "eg", "sv", "gq", "er", "ee", "et", "fk", "fo", "fj", "fi", "fr", "gf", "pf", "tf", "ga", "gm", "ge", "de", "gh", "gi", "gr", "gl", "gd", "gp", "gu", "gt", "gn", "gw", "gy", "ht", "hm", "va", "hn", "hk", "hu", "is", "in", "id", "ir", "iq", "ie", "il", "it", "jm", "jp", "jo", "kz", "ke", "ki", "kp", "kr", "kw", "kg", "la", "lv", "lb", "ls", "lr", "ly", "li", "lt", "lu", "mo", "mk", "mg", "mw", "my", "mv", "ml", "mt", "mh", "mq", "mr", "mu", "yt", "mx", "fm", "md", "mc", "mn", "ms", "ma", "mz", "mm", "na", "nr", "np", "nl", "an", "nc", "nz", "ni", "ne", "ng", "nu", "nf", "mp", "no", "om", "pk", "pw", "ps", "pa", "pg", "py", "pe", "ph", "pn", "pl", "pt", "pr", "qa", "re", "ro", "ru", "rw", "sh", "kn", "lc", "pm", "vc", "ws", "sm", "st", "sa", "sn", "cs", "sc", "sl", "sg", "sk", "si", "sb", "so", "za", "gs", "es", "lk", "sd", "sr", "sj", "sz", "se", "ch", "sy", "tw", "tj", "tz", "th", "tl", "tg", "tk", "to", "tt", "tn", "tr", "tm", "tc", "tv", "ug", "ua", "ae", "gb", "us", "um", "uy", "uz", "vu", "ve", "vn", "vg", "vi", "wf", "eh", "ye", "zm", "zw" }; } as customizing listview, have create class extending baseadapter, working controller of listview.
so here efficientadapter.java file :
private static class efficientadapter extends baseadapter { private layoutinflater minflater; public efficientadapter(context context) { minflater = layoutinflater.from(context); } public int getcount() { return countrieslist.abbreviations.length; } public object getitem(int position) { return position; } public long getitemid(int position) { return position; } public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { convertview = minflater.inflate(r.layout.row, null); holder = new viewholder(); holder.text1 = (textview) convertview .findviewbyid(r.id.textview01); holder.text2 = (textview) convertview .findviewbyid(r.id.textview02); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.text1.settext(countrieslist.abbreviations[position]); holder.text2.settext(countrieslist.countries[position]); return convertview; } static class viewholder { textview text1; textview text2; } } now last thing remaining activity class. here :
public class mylist extends activity { /** called when activity first created. */ listview listview; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); listview = (listview) findviewbyid(r.id.lv_country); listview.setadapter(new efficientadapter(this)); listview.setonitemclicklistener( new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view view,int position, long arg3) { // todo auto-generated method stub toast.maketext(getapplicationcontext(),"you selected item "+integer.tostring(position), toast.length_long).show(); } } ); }
Comments
Post a Comment