android - How can I use OnListItemClickListener using this Search Filtering ListView -
i'm having trouble using setonitemclicklistener in listview. everytime try, causes many errors fixes makes code worse i'm intending do.
this code:
public class search extends activity{ // list view private listview lv; // listview adapter arrayadapter<string> adapter; // search edittext edittext inputsearch; // arraylist listview arraylist<hashmap<string, string>> productlist; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_search); // listview data final string products[] = {"fill in blocks", "the music bee", "little paragon", "subway surfers", "cytus", "temple run", "clumsy ninja", "smash hit"}; lv = (listview) findviewbyid(r.id.list_view); inputsearch = (edittext) findviewbyid(r.id.inputsearch); // adding items listview adapter = new arrayadapter<string>(this, r.layout.list_item, r.id.product_name, products); lv.setadapter(adapter); /** * enabling search filter * */ inputsearch.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence cs, int arg1, int arg2, int arg3) { // when user changed text search.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 } }); } }
i want make strings in listview data clickable other activities won't work. tried code:
lv.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view view, int position, long id) { string openclass = adapter.getitem[position]; if (openclass.equals("fill in blocks")) { intent myintent = new intent(view.getcontext(), fill_in_the_blocks.class); startactivityforresult(myintent, 0); } else if (openclass.equals("the music bee")) { intent myintent1 = new intent(view.getcontext(), the_music_bee.class); startactivityforresult(myintent1, 0); } else if (openclass.equals("little paragon")) { intent myintent2 = new intent(view.getcontext(), little_paragon.class); startactivityforresult(myintent2, 0); } }
the errors follows: -in lv.setonitemclicklistener, "the method setonitemclicklistener(adapterview.onitemclicklistener) in type adapterview not applicable arguments (new onitemclicklistener(){})" -in (new onitemclicklistener(), "onitemclicklistener cannot resolved type" -in public void onitemclick(adapterview, "adapterview cannot resolved type" -in string openclass = adapter.getitem[position];, "getitem cannot resolved or not field"
- in lv.setonitemclicklistener, "the method setonitemclicklistener(adapterview.onitemclicklistener) in type adapterview not applicable arguments (new onitemclicklistener(){})"
- in (new onitemclicklistener(), "onitemclicklistener cannot resolved type"
- in public void onitemclick(adapterview, "adapterview cannot resolved type"
the first 3 errors should disappear replacing new onitemclicklistener() with
new adapterview.onitemclicklistener()
and make sure import:
import android.widget.adapterview;
the final error:
- in string openclass = adapter.getitem[position];, "getitem cannot resolved or not field"
you meant calling getitem method, rather using array indexing.
replace line:
string openclass = adapter.getitem[position];
with
string openclass = adapter.getitem(position);
Comments
Post a Comment