android - Where am I doing it wrong: CursorAdapter and Listview -
my first app.
when click on row 1's button expect hide textview
particular row. when scroll down see has hidden other `textview's too. read bit , found expect recycling takes place.
now, wondering how make sure particular row's textview
hidden when button clicked. may have many rows as 7000+
below cursoradapter
code:
public class todocursoradapter extends cursoradapter { public todocursoradapter(context context, cursor cursor) { super(context, cursor, 0); } // newview method used inflate new view , return it, // don't bind data view @ point. @override public view newview(context context, cursor cursor, viewgroup parent) { return layoutinflater.from(context).inflate(r.layout.listview, parent, false); } @override public void bindview(view view, final context context, cursor cursor) { final textview engtext = (textview) view.findviewbyid(r.id.engtext); final textview arabtext = (textview) view.findviewbyid(r.id.arabtext); final textview reftext = (textview) view.findviewbyid(r.id.reftext); button buttonia = (button) view.findviewbyid(r.id.buttonia); //for arabic text button buttonir = (button) view.findviewbyid(r.id.buttonir); //for ref text string arabic = cursor.getstring(cursor.getcolumnindexorthrow("plainarab_text")); string english = cursor.getstring(cursor.getcolumnindexorthrow("plaineng_text")); string ref = cursor.getstring(cursor.getcolumnindexorthrow("ref")); arabtext.settext(arabic.trim().replaceall(" +", " ")); engtext.settext(english.replaceall("[\n]{2,}", "twofeeds").replaceall("\n", " ").replaceall(" +", " ").replaceall("<br/>", "\n").replaceall("twofeeds", "\n")); reftext.settext(ref.trim().replaceall(" +", " ")); arabtext.setvisibility(view.visible); engtext.setvisibility(view.visible); buttonia.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { arabtext.setvisibility(view.gone); } }); buttonir.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { reftext.setvisibility(view.gone); log.d("mytag", "inside2"); } }); } }
activity
code:
public class listviewactivity extends activity { listview listview ; private sqlitedatabase hadithlistdb; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_listview); ..... // find listview populate listview lvitems = (listview) findviewbyid(r.id.list); // setup cursor adapter using cursor last step todocursoradapter todoadapter = new todocursoradapter(this, todocursor); // attach cursor adapter listview lvitems.setadapter(todoadapter); todoadapter.changecursor(todocursor); } }
updated
ok. inserted debug comment/message in app see how cursoradapter
works. when scroll , down show row# or record number(in case) in debug comment.
so came idea tag views created , when click on button on row(say 1) should hide textview
on row 1 plus tag cursor position should added array.
the logic have implemented far prevent other rows being affected button.
i clicked button1 , hid textview1 on row 1. scroll down see if affected other rows, hadn't. scroll , see textview1 on row 1 visible on row 2 hidden.
can see wrong code:
public class todocursoradapter extends cursoradapter { list<integer> selecteditemspositions;//to store selected items position public todocursoradapter(context context, cursor cursor) { super(context, cursor, 0); selecteditemspositions = new arraylist<>(); } @override public view newview(context context, cursor cursor, viewgroup parent) { //viewholderitem viewholder; view rowview = layoutinflater.from(context).inflate(r.layout.listview, parent, false); rowview.settag(cursor.getposition()); return rowview; } @override public void bindview(final view view, final context context, final cursor cursor) { final textview arabtext = (textview) view.findviewbyid(r.id.arabtext); final textview engtext = (textview) view.findviewbyid(r.id.engtext); final textview reftext = (textview) view.findviewbyid(r.id.reftext); button buttonia = (button) view.findviewbyid(r.id.buttonia); //for arabic text button buttonir = (button) view.findviewbyid(r.id.buttonir); //for ref text string arabic = cursor.getstring(cursor.getcolumnindexorthrow("plainarab_text")); string english = cursor.getstring(cursor.getcolumnindexorthrow("plaineng_text")); string ref = cursor.getstring(cursor.getcolumnindexorthrow("ref")); arabtext.settext(arabic.trim().replaceall(" +", " ")); engtext.settext(english.replaceall("[\n]{2,}", "twofeeds").replaceall("\n", " ").replaceall(" +", " ").replaceall("<br/>", "\n").replaceall("twofeeds", "\n")); reftext.settext(ref.trim().replaceall(" +", " ")); view.settag(cursor.getposition()); if(selecteditemspositions.contains(cursor.getposition())) { arabtext.setvisibility(view.gone); } else { arabtext.setvisibility(view.visible); } buttonia.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { integer position = (integer) v.gettag(); if(selecteditemspositions.contains(position)) { arabtext.setvisibility(view.visible); selecteditemspositions.remove((object) cursor.getposition()); } else { arabtext.setvisibility(view.gone); log.d("mytag", "clicke! inside else"); selecteditemspositions.add( cursor.getposition()); } } } ); buttonir.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { log.d("mytag", "inside2"); } } ); }
}
i able fix issue overriding getview
method position value of cell, using getposition
of cursor bit messy updates every newview
call more info, still calling getview
, newview
inflate view twice (need tweaking there)
here final code (this code resembles other question, didn't have time update it)
public class todocursoradapter extends cursoradapter { list<integer> selectedarabtextitemspositions;//to store selected items position context context; public todocursoradapter(context context, cursor cursor) { super(context, cursor, 0); selectedarabtextitemspositions = new arraylist<>(); this.context = context; } // newview method used inflate new view , return it, // don't bind data view @ point. @override public view newview(context context, cursor cursor, viewgroup parent) { return layoutinflater.from(context).inflate(r.layout.activity_view_record, parent, false); } @override public view getview(int position, view convertview, viewgroup arg2) { if (convertview == null) { layoutinflater inflater = layoutinflater.from(context); convertview = inflater.inflate(r.layout.activity_view_record, null); } convertview.settag(position); return super.getview(position, convertview, arg2); } @override public void bindview(final view view, context context, final cursor cursor) { log.d("test",string.valueof((integer) view.gettag())); final textview arabtext; final textview engtext; final textview reftext; final button button; final button button2; engtext = (textview) view.findviewbyid(r.id.engtext); arabtext = (textview) view.findviewbyid(r.id.arabtext); reftext = (textview) view.findviewbyid(r.id.reftext); button = (button) view.findviewbyid(r.id.button); button2 = (button) view.findviewbyid(r.id.button2); string arabic = cursor.getstring(cursor.getcolumnindexorthrow("plainarab_text")); string english = cursor.getstring(cursor.getcolumnindexorthrow("plaineng_text")); string ref = cursor.getstring(cursor.getcolumnindexorthrow("ref")); arabtext.settext(arabic); engtext.settext(english); reftext.settext(ref); if(selectedarabtextitemspositions.contains((integer) view.gettag())) { arabtext.setvisibility(view.gone); } else { arabtext.setvisibility(view.visible); } button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v){ if (selectedarabtextitemspositions.contains((integer) view.gettag())){ arabtext.setvisibility(view.visible); log.d("selected contain id>>>>>>>>",string.valueof(string.valueof((integer) view.gettag()))); removeitemfromlist((integer) view.gettag(),selectedarabtextitemspositions); }else { arabtext.setvisibility(view.gone); log.d("selected text not contain id>>>>>>>>",string.valueof((integer) view.gettag())); selectedarabtextitemspositions.add((integer) view.gettag()); } log.d("list content >>>>",string.valueof(selectedarabtextitemspositions)); } }); button2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v){ //viewholder.engtext.setvisibility(view.gone); } }); } void removeitemfromlist (int object,list arraylist){ iterator<integer> = arraylist.iterator(); while (it.hasnext()) { if (it.next() == object) { it.remove(); } } } }
Comments
Post a Comment