Android Autocompletion using AutocompleteTextView
Autocompletion:
Some notes:
- XML Parsing
- ArrayAdapter for adapting the listview visual display
- ListView iconized approach: https://w2davids.wordpress.com/android-listview-with-iconsimages-and-sharks-with-lasers/
NOTE: Latest Updated version :
https://w2davids.wordpress.com/android-auto-completionwith-icons/
File: main.xml
<!--?xml version="1.0" encoding="utf-8"?-->
File: country_item.xml
<!--?xml version="1.0" encoding="utf-8"?-->
File: Main.java
import java.io.InputStream; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; public class Main extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listview); setTitle("TestAutoCompletion"); // Parse countries CountryParser countryParser = new CountryParser(); InputStream inputStream = getResources().openRawResource(R.raw.countries); countryParser.parse(inputStream); // Get Countries List countryList = getCountryList(countryParser.getList()); AutoCompleteTextView autoSearch = (AutoCompleteTextView) this.findViewById(R.id.auto_search); ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), R.layout.country_item, countryList); autoSearch.setAdapter(adapter); // Threshold for how many items to display in dropdown list autoSearch.setThreshold(1); } private List getCountryList(List countries) { List list = new ArrayList(); for (Country c : countries) { list.add(c.name); } return list; } }
File: Country.java
public class Country { public String name; public String abbreviation; String region; public String resourceId; public Country() { // TODO Auto-generated constructor stub } public Country(String name, String abbreviation, String region, String resourceFilePath) { this.name = name; this.abbreviation = abbreviation; this.region= region; this.resourceId = resourceFilePath; } @Override public String toString() { return this.name; } }
File: CountryParser.java
import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import android.util.Log; public class CountryParser { private static final String tag = "CountryParser"; private DocumentBuilderFactory factory; private DocumentBuilder builder; private final HashMap map; private final List list; public CountryParser() { this.list = new ArrayList(); this.map = new HashMap(); } private String getNodeValue(NamedNodeMap map, String key) { String nodeValue = null; Node node = map.getNamedItem(key); if (node != null) { nodeValue = node.getNodeValue(); } return nodeValue; } public List getList() { return this.list; } public String getAbbreviation(String country) { return this.map.get(country); } /** * Parse XML file containing body part X/Y/Description * * @param inStream */ public void parse(InputStream inStream) { try { // TODO: after we must do a cache of this XML!!!! this.factory = DocumentBuilderFactory.newInstance(); this.builder = this.factory.newDocumentBuilder(); this.builder.isValidating(); Document doc = this.builder.parse(inStream, null); doc.getDocumentElement().normalize(); NodeList countryList = doc.getElementsByTagName("country"); final int length = countryList.getLength(); for (int i = 0; i < length; i++) { final NamedNodeMap attr = countryList.item(i).getAttributes(); final String countryName = getNodeValue(attr, "name"); final String countryAbbr = getNodeValue(attr, "abbreviation"); final String countryRegion = getNodeValue(attr, "region"); Country country = new Country(countryName, countryAbbr, countryRegion, countryAbbr + ".png"); this.list.add(country); this.map.put(countryName, countryAbbr); Log.d(tag, country.toString()); } } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } } }
..mmh XML view layouts are being cut-off!
xml views are being cut-off!
0 Pingbacks