android - How to setup Listview to strikethrough item after click? -
i have list of strings setup on listview in on create:
//set adapter lvadapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, chosenarray); //add listview lw.setadapter(lvadapter);
my question:
i want setup list once item clicked, gets strike through. however, new android , haven't done before.
what i've tried unsuccessfully:
based on answer tried method, cross off items not visible in list well.
lw.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { //cross off textview text = (textview) view; text.setpaintflags(text.getpaintflags() | paint.strike_thru_text_flag); } });
i've read there maybe way extend arrayadapter, have never done before. ideas?
update: since we're running issues, here's whole code. it's within oncreate:
public arrayadapter<string> lvadapter; public arraylist<string> arrplayers = new arraylist<string>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_pool_sheet); //setup list view listview lw = (listview) findviewbyid(r.id.lvbouts); //adapter use on listview string[] chosenarray = {""}; //convert if (extras != null) { arrplayers = extras.getstringarraylist("arrplayers"); } //based on array input size main activity //select pre defined string arrays switch (arrplayers.size()) { case 6: chosenarray = poolofsix; break; case 7: chosenarray = poolofseven; break; case 8: chosenarray = poolofeight; break; case 9: chosenarray = poolofnine; break; case 10: chosenarray = pooloften; break; //no need default since multiple choice } //set adapter lvadapter = new arrayadapter<string>this,android.r.layout.simple_list_item_1, chosenarray); lw.setadapter(lvadapter); //list view onclick listener lw.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { //cross off textview text = (textview) view; text.setpaintflags(text.getpaintflags() | paint.strike_thru_text_flag); }); }
here's behavior has:
update:
you want use custom adapter then. instantiate list view this:
adapter = new customadapter(yourarrayhere); listview.setadapter(adapter);
then create custom adapter so:
private class customadapter extends baseadapter { private string[] strings; private arraylist<string> selectedstrings; public customadapter(string[] strings) { this.strings = strings; selectedstrings = new arraylist<>(); } @override public int getcount() { return strings.length; } @override public string getitem(int i) { return strings[i]; } @override public long getitemid(int i) { return 0; } @override public view getview(int i, view view, viewgroup viewgroup) { viewholder holder; if (view == null) { holder = new viewholder(); view = layoutinflater.from(viewgroup.getcontext()).inflate(android.r.layout.simple_list_item_1, viewgroup, false); holder.text = (textview) view; view.settag(holder); } else { holder = (viewholder) view.gettag(); } holder.text.settext(getitem(i)); if (selectedstrings.contains(getitem(i))) { holder.text.setpaintflags(holder.text.getpaintflags() | paint.strike_thru_text_flag); } else { holder.text.setpaintflags(0); } return view; } private class viewholder { textview text; } public arraylist<string> getselectedstrings() { return selectedstrings; } }
then new click like:
@override public void onitemclick(adapterview<?> adapterview, view view, int i, long l) { adapter.getselectedstrings().add(list[i]); adapter.notifydatasetchanged(); }
now selectedstrings array list contains of strings have selected.
try this:
@override public void onitemclick(adapterview<?> adapterview, view view, int i, long l) { textview text = (textview) view; text.setpaintflags(text.getpaintflags() | paint.strike_thru_text_flag); }
the first line getting row's view (which text view, due using simple list item 1). if using custom view, layout images , text view, need add:
textview text = (textview)view.findviewbyid(r.id.my_text_view);
Comments
Post a Comment