@Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); lv1 = (ListView) findViewById(R.id.ListView01); ed = (AutoCompleteTextView) findViewById(R.id.EditTextSearch); // AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country); ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, R.layout.list_item, countryName); ed.setAdapter(adapter1); this.getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); final List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>(); for (int i = 0; i < countryName.length; i++) { HashMap<String, String> map = new HashMap<String, String>(); map.put("flag", "" + imageId[i]); map.put("country", countryName[i].toString()); map.put("capital", capitalName[i].toString()); map.put("countrytime", convertDateTimeToGMT(GMTplusMinusInMillisecond[i], plusMinus[i])); map.put("GMT", GMTplusMinus[i].toString()); fillMaps.add(map); } // fill in the grid_item layout SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to); lv1.setAdapter(adapter); ed.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) { fillMaps.clear(); textlength = ed.getText().length(); for (int i = 0; i < countryName.length; i++) { if (textlength <= countryName[i].length()) { if (ed.getText() .toString() .equalsIgnoreCase( (String) countryName[i].subSequence(0, textlength))) { HashMap<String, String> map = new HashMap<String, String>(); map.put("flag", "" + imageId[i]); map.put("country", countryName[i].toString()); map.put("capital", capitalName[i].toString()); map.put("countrytime", convertDateTimeToGMT( GMTplusMinusInMillisecond[i], plusMinus[i])); map.put("GMT", GMTplusMinus[i].toString()); fillMaps.add(map); } } } if(!fillMaps.isEmpty()) { SimpleAdapter adapter = new SimpleAdapter( WorldClockActivity.this, fillMaps, R.layout.grid_item, from, to); lv1.setAdapter(adapter); } else { String[] COUNTRIES = new String[] {"No record found"}; lv1.setAdapter(new ArrayAdapter<String>(WorldClockActivity.this,R.layout.list_item, COUNTRIES)); } // lv1.setAdapter(new // ArrayAdapter<String>(WorldClockActivity.this,android.R.layout.simple_list_item_1 // , arr_sort)); } }); } public static String convertDateTimeToGMT(long millis, int plusMinus) { Calendar CalGMT; TimeZone.setDefault(TimeZone.getTimeZone("GMT")); CalGMT = new GregorianCalendar(TimeZone.getTimeZone("GMT")); CalGMT.get(Calendar.DAY_OF_MONTH); CalGMT.get(Calendar.MONTH); CalGMT.get(Calendar.YEAR); CalGMT.get(Calendar.HOUR_OF_DAY); CalGMT.get(Calendar.MINUTE); CalGMT.get(Calendar.SECOND); if (plusMinus == 1) { CalGMT.setTimeInMillis(CalGMT.getTimeInMillis() + millis); } else if (plusMinus == 0) { CalGMT.setTimeInMillis(CalGMT.getTimeInMillis() - millis); } String sendDateTimeInGMT = CalGMT.get(Calendar.HOUR_OF_DAY) + ":" + CalGMT.get(Calendar.MINUTE) + ":" + CalGMT.get(Calendar.SECOND); return sendDateTimeInGMT; }
}
I made an application using the above code in this application. I have an AutoCompleteTextView function to allow search in a list, then the list view displays the name of the entire country and a user who can search for a country by country name, when a user enters autostart in AutoCompleteTextView, the corresponding search is displayed in the list. Ex, if the user wants to find Canada in the list of countries of the world, then the user enters only ca in the AutoCompleteTextView, then another list appears reflecting the AutoCompleteTextView and showing the country name for the start of the name ca, then the user selects Canada in this then enter all the Canada information in the list .