i want develop "history of country" android application. have problem. can not put country header in listview list item. want put header; such "africa : north africa", "sub-saharan africa", "asia", "north asia", "west & central asia", "south & southeast asia", "europe", "north & central america", "north america", "oceania", "south america".
here source code :
https://www.dropbox.com/s/k66xonom9up1yif/country%20search%20listview.zip?dl=0
here sample code:
search_country.java
package com.nasir.search; import java.util.arraylist; import java.util.arrays; import android.app.activity; import android.app.listactivity; import android.content.intent; import android.os.bundle; import android.text.editable; import android.text.textwatcher; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.baseadapter; import android.widget.edittext; import android.widget.listview; import android.widget.textview; public class search_country extends listactivity { private edittext searchtext; private listview listtext; private string[] number_list = { // africa : north africa "algeria", "egypt", "libya", "morocco", "tunisia", "western sahara", // sub-saharan africa "angola", "benin", "botswana", "burkina faso", "burundi", "cameroon", "cape verde", "chad", "comoros", "congo", "côte d'ivoire", "djibouti", "equatorial guinea", "eritrea", "ethiopia", "gabon", "gambia", "ghana", "guinea", "kenya", "lesotho", "liberia", "madagascar", "malawi", "mali", "mauritania", "mauritius", "mayotte", "mozambique", "namibia", "niger", "nigeria", "rwanda", "senegal", "seychelles", "sierra leone", "somalia", "south africa", "south sudan", "sudan", "swaziland", "uganda", "zambia", "zimbabwe", // asia : // east asia "china", "hong kong", "japan", "korea", "macao", "mongolia", "taiwan", // north asia "belarus", "moldova", "russian federation", "ukraine", // west & central asia "afghanistan", "armenia", "azerbaijan", "bahrain", "cyprus", "georgia", "iran", "iraq", "israel", "jordan", "kazakhstan", "kuwait", "kyrgyzstan", "lebanon", "oman", "pakistan", "qatar", "saudi arabia", "tajikistan", "turkey", "turkmenistan", "united arab emirates", "uzbekistan", "yemen", // south & southeast asia "bangladesh", "bhutan", "brunei", "cambodia", "india", "indonesia", "malaysia", "maldives", "myanmar", "nepal", "philippines", "singapore", "sri lanka", "thailand", "timor-leste", "viet nam", // europe "Åland islands", "albania", "andorra", "austria", "belgium", "bosnia , herzegovina", "bulgaria", "croatia", "czech republic", "denmark", "estonia", "faroe islands", "finland", "france", "germany", "gibraltar", "greece", "greenland", "guernsey", "hungary", "iceland", "", "", "", "", "ireland", "jersey", "latvia", "liechtenstein", "lithuania", "luxembourg", "macedonia", "monaco", "montenegro", "netherlands", "norway", "poland", "portugal", "romania", "san marino", "serbia", "slovakia", "slovenia", "spain", "svalbard , jan mayen", "sweden", "switzerland", "united kingdom", // north & central america "caribbean islands", "anguilla", "antigua , barbuda", "aruba", "bahamas", "barbados", "bermuda", "cayman islands", "bonaire", "cuba", "dominica", "curaçao", "guadeloupe", "haiti", "jamaica", "martinique", "montserrat", "puerto rico", "saint bathélemy", "saint kitts , nevis", "saint lucia", "saint martin (french part)", "saint vincent , grenadines", "sint maarten (dutch part)", "trinidad , tobago", "turks , caicos islands", "virgin islands, british", "virgin islands, u.s.", // mesoamerica (or central america) "belize", "costa rica", "el salvador", "guatemala", "honduras", "mexico", "nicaragua", "panama", // north america "canada", "saint pierre , miquelon", "united states", // oceania "american samoa", "australia", "christmas island", "cocos (keeling) islands", "cook islands", "fiji", "guam", "kiribati", "marshall islands", "micronesia", "new caledonia", "new zealand", "niue", "norfolk island", "northern mariana islands", "palau", "papua new guinea", "pitcairn", "samoa", "solomon islands", "tokelau", "tonga", "tuvalu", "united states minor outlying islands", "vanuatu", "wallis , futuna", // south america "argentina", "bolivia", "brazil", "chile", "colombia", "ecuador", "falkland islands (malvinas)", "french guiana", "guyana", "paraguay", "peru", "suriname", "uruguay", "venezuela"}; private arraylist<string> array_sort; int textlength = 0; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.search_country); searchtext = (edittext) findviewbyid(r.id.listview_search); listtext = (listview) findviewbyid(android.r.id.list); array_sort = new arraylist<string>(arrays.aslist(number_list)); setlistadapter(new bsadapter(this)); searchtext.addtextchangedlistener(new textwatcher() { public void aftertextchanged(editable s) { } public void beforetextchanged(charsequence s, int start, int count, int after) { } public void ontextchanged(charsequence s, int start, int before, int count) { textlength = searchtext.gettext().length(); array_sort.clear(); for (int = 0; < number_list.length; i++) { if (textlength <= number_list[i].length()) { if(number_list[i].touppercase().contains(searchtext.gettext().tostring().touppercase().trim())) { array_sort.add(number_list[i]); } } } appendlist(array_sort); } }); listtext.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view view, int position, long id) { if( array_sort.get(position).equals("afghanistan")){ intent intent = new intent(search_country.this, country_details.class); intent.putextra("header", getstring(r.string.html_afganistan)); startactivity(intent); } else if ( array_sort.get(position).equals("albania")){ intent intent = new intent(search_country.this, country_details.class); intent.putextra("header", getstring(r.string.html_albenia)); startactivity(intent); } else if ( array_sort.get(position).equals("algeria")){ intent intent = new intent(search_country.this, country_details.class); intent.putextra("header", getstring(r.string.html_algeria)); startactivity(intent); } else if ( array_sort.get(position).equals("brazil")){ intent intent = new intent(search_country.this, country_details.class); intent.putextra("header", getstring(r.string.html_brazil)); startactivity(intent); } } }); } public void appendlist(arraylist<string> str) { setlistadapter(new bsadapter(this)); } public class bsadapter extends baseadapter { activity cntx; public bsadapter(activity context) { this.cntx = context; } public int getcount() { return array_sort.size(); } public object getitem(int position) { return array_sort.get(position); } public long getitemid(int position) { return array_sort.size(); } public view getview(final int position, view convertview, viewgroup parent) { view row = null; layoutinflater inflater = cntx.getlayoutinflater(); row = inflater.inflate(r.layout.search_country_listview, null); textview tv = (textview) row.findviewbyid(r.id.listview_seacrh_text); tv.settext(array_sort.get(position)); return row; } } }
screen shot

activity_main.xml
<linearlayout 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" android:orientation="vertical" > <edittext android:id="@+id/edtsearch" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:hint="search..." android:padding="10dp" /> <listview android:id="@+id/lvcountry" android:layout_width="match_parent" android:layout_height="match_parent" > </listview> </linearlayout> layout_item.xml
<?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="vertical" > <textview android:id="@+id/tvitemtitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="item" /> </linearlayout> layout_section.xml
<?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:background="#d8d8d8" android:orientation="vertical" > <textview android:id="@+id/tvsectiontitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:padding="10dp" android:text="section" android:textstyle="bold" /> </linearlayout> mainactivity.java
public class mainactivity extends actionbaractivity { private listview lvcountry; private edittext edtsearch; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); edtsearch = (edittext) findviewbyid(r.id.edtsearch); lvcountry = (listview) findviewbyid(r.id.lvcountry); arraylist<item> countrylist = new arraylist<mainactivity.item>(); // header countrylist.add(new sectionitem("asia")); // state name countrylist.add(new entryitem("india")); countrylist.add(new entryitem("china")); countrylist.add(new entryitem("hong kong")); countrylist.add(new entryitem("nepal")); // header countrylist.add(new sectionitem("north asia")); // state name countrylist.add(new entryitem("belarus")); countrylist.add(new entryitem("moldova")); countrylist.add(new entryitem("russian federation")); countrylist.add(new entryitem("ukraine")); // header countrylist.add(new sectionitem("north america")); // state name countrylist.add(new entryitem("canada")); countrylist.add(new entryitem("saint pierre , miquelon")); countrylist.add(new entryitem("united states")); // header countrylist.add(new sectionitem("north & central america")); // state name countrylist.add(new entryitem("caribbean islands")); countrylist.add(new entryitem("anguilla")); countrylist.add(new entryitem("antigua , barbuda")); countrylist.add(new entryitem("aruba")); // set adapter final countryadapter adapter = new countryadapter(this, countrylist); lvcountry.setadapter(adapter); lvcountry.settextfilterenabled(true); // filter on text change edtsearch.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence s, int start, int before, int count) { if(adapter != null) { adapter.getfilter().filter(s.tostring()); } } @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void aftertextchanged(editable s) { } }); } /** * row item */ public interface item { public boolean issection(); public string gettitle(); } /** * section item */ public class sectionitem implements item { private final string title; public sectionitem(string title) { this.title = title; } public string gettitle() { return title; } @override public boolean issection() { return true; } } /** * entry item */ public class entryitem implements item { public final string title; public entryitem(string title) { this.title = title; } public string gettitle() { return title; } @override public boolean issection() { return false; } } /** * adapter */ public class countryadapter extends baseadapter { private context context; private arraylist<item> item; private arraylist<item> originalitem; public countryadapter() { super(); } public countryadapter(context context, arraylist<item> item) { this.context = context; this.item = item; //this.originalitem = item; } @override public int getcount() { return item.size(); } @override public object getitem(int position) { return item.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); if (item.get(position).issection()) { // if section header convertview = inflater.inflate(r.layout.layout_section, parent, false); textview tvsectiontitle = (textview) convertview.findviewbyid(r.id.tvsectiontitle); tvsectiontitle.settext(((sectionitem) item.get(position)).gettitle()); } else { // if item convertview = inflater.inflate(r.layout.layout_item, parent, false); textview tvitemtitle = (textview) convertview.findviewbyid(r.id.tvitemtitle); tvitemtitle.settext(((entryitem) item.get(position)).gettitle()); } return convertview; } /** * filter */ public filter getfilter() { filter filter = new filter() { @suppresswarnings("unchecked") @override protected void publishresults(charsequence constraint, filterresults results) { item = (arraylist<item>) results.values; notifydatasetchanged(); } @suppresswarnings("null") @override protected filterresults performfiltering(charsequence constraint) { filterresults results = new filterresults(); arraylist<item> filteredarraylist = new arraylist<item>(); if(originalitem == null || originalitem.size() == 0) { originalitem = new arraylist<item>(item); } /* * if constraint null return original value * else return filtered value */ if(constraint == null && constraint.length() == 0) { results.count = originalitem.size(); results.values = originalitem; } else { constraint = constraint.tostring().tolowercase(locale.english); (int = 0; < originalitem.size(); i++) { string title = originalitem.get(i).gettitle().tolowercase(locale.english); if(title.startswith(constraint.tostring())) { filteredarraylist.add(originalitem.get(i)); } } results.count = filteredarraylist.size(); results.values = filteredarraylist; } return results; } }; return filter; } } } androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sectionheader" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="11" android:targetsdkversion="21" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:windowsoftinputmode="statehidden|adjustpan" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
Comments
Post a Comment