onitemclicklistener - Search in ListView android -
i'm using following class search in listview of countries
package com.androidhive.androidlistviewwithsearch; import java.util.arraylist; import java.util.hashmap; import java.util.list; import android.app.activity; import android.content.context; import android.content.intent; import android.database.cursor; import android.os.bundle; import android.text.editable; import android.text.textwatcher; import android.view.inputmethod.inputmethodmanager; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.arrayadapter; import android.widget.edittext; import android.widget.listview; import android.widget.simplecursoradapter; public class mainactivity extends activity { private listview lv; simplecursoradapter cursoradapter; arrayadapter<string> adapter; edittext inputsearch; private sqliteadapter mysqliteadapter; arraylist<hashmap<string, string>> productlist; public static final string country_name = "country_name"; string[] countryname; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mysqliteadapter = new sqliteadapter(this); mysqliteadapter.opentowrite(); int isempty = mysqliteadapter.isempty(); if (isempty == 0) { mysqliteadapter.insert("afghanistan", "102", "119", "119"); mysqliteadapter.insert("albania", "127", "128", "129"); mysqliteadapter.insert("algeria", "14", "14", "17"); mysqliteadapter.insert("american samoa", "911", "911", "911"); mysqliteadapter.insert("andorra", "118", "118", "110"); mysqliteadapter.insert("angola", "118", "118", "110"); mysqliteadapter.insert("panama", "911", "911", "911"); mysqliteadapter.insert("papua new guinea /port moresby", "", "110", "000"); mysqliteadapter.insert("yemen", "191", "191", "194"); mysqliteadapter.insert("zambia", "991/112", "993/112", "999/112"); mysqliteadapter.insert("zimbabwe", "994/999", "993/999", "995/999"); } mysqliteadapter = new sqliteadapter(this); mysqliteadapter.opentoread(); cursor cursor = mysqliteadapter.queueall(); startmanagingcursor(cursor); list<string> list = new arraylist<string>(); if (cursor.movetofirst()) { while (cursor.isafterlast() == false) { string countries = cursor.getstring(cursor.getcolumnindex(sqliteadapter.countries_content)).tostring().trim(); system.out.println("countries: " + countries); list.add(countries); cursor.movetonext(); } } countryname = new string[list.size()]; countryname = list.toarray(countryname); mysqliteadapter.close(); lv = (listview) findviewbyid(r.id.list_view); inputsearch = (edittext) findviewbyid(r.id.inputsearch); adapter = new arrayadapter<string>(this, r.layout.list_item, r.id.product_name, countryname); lv.setadapter(adapter); lv.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parentview, view childview, int position, long id) { intent intent ; bundle b = new bundle(); b.putstring(country_name, countryname[position]); intent = new intent(getapplicationcontext(), countrynumbers.class); intent.putextras(b); startactivity(intent); finish(); } }); inputsearch.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence cs, int arg1, int arg2, int arg3) { // when user changed text mainactivity.this.adapter.getfilter().filter(cs); } @override public void beforetextchanged(charsequence arg0, int arg1, int arg2, int arg3) { // todo auto-generated method stub } @override public void aftertextchanged(editable arg0) { // todo auto-generated method stub } }); } }
this code works , search , setonitemclicklistener opens activity contains info selected country. when search , wrote "e" example, found list changed , gets countries starts "e" when press example second country gets search, opens country second index in "countryname" array. how can solve issue , info of selected country search?
hope got mean. in advance.
i solved replacing line
b.putstring(country_name, countryname[position]);
by line
b.putstring(country_name, adapter.getitem(position));
Comments
Post a Comment