android - Why is the first line always crossed out? -
in application use simplecursoradapter
. if checkbox.ischecked()
text should strikethrough. why first line crossed out, if checkbox
not checked
public class mainactivity extends activity { // button btncalendar; //*******************************************8 string[] names = {"Иван", "Марья", "Петр", "Антон", "Даша", "Борис", "Костя", "Игорь", "Анна", "Денис", "Андрей"}; //button buttonaddtask; final string tag="states"; final string ten = "ten"; textview txtdatatasktoday; string id_for_listtsk_today; listview lvmain_today; string[] arr_date; sharedpreferences spref; static cursor c; private listview listview = null; sqlitedatabase db; //public static string id_for_listtsk_today; // static sqlitedatabase db; mysqlcursoradapter adapter = null; //***********************************************8 @override protected void oncreate(bundle savedinstancestate) { if (getintent().getbooleanextra("finish", false)) finish(); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // btncalendar = (button) findviewbyid(r.id.btnacttwo); // btncalendar.setonclicklistener(this); //********************************************* // переменные для query string[] columns = null; string selection = null; string[] selectionargs = null; string groupby = null; string having = null; string orderby = null; //*********работа с БД**************** // создаем объект для данных txtdatatasktoday = (textview) findviewbyid(r.id.txtdatatasktoday); simpledateformat sdf = new simpledateformat("yyyy-mm-dd"); string id_for_listtsk_today = sdf.format(new date()); //final string b = id_for_listtsk_today; txtdatatasktoday.settext("today: "+id_for_listtsk_today.tostring()); // txtdatatasktoday.setpaintflags(txtdatatasktoday.getpaintflags() | paint.strike_thru_text_flag); log.d(tag, "id_for_listtsk_today ="+id_for_listtsk_today ); contentvalues cv = new contentvalues(); dbhelper dbhelper = new dbhelper(this); final sqlitedatabase db = dbhelper.getwritabledatabase(); columns = new string[] {"name"}; selection = "data_id = ?"; selectionargs = new string[] {id_for_listtsk_today}; //c = db.query("mytable", columns, selection, selectionargs, null, null, null); try { c=dbhelper.getcursor(id_for_listtsk_today); } catch (sqlexception sqle) { log.d(tag, "неудача"); throw sqle; } string[] arr_date = logcursor(c); //*********работа с БД**************** lvmain_today = (listview) findviewbyid(r.id.list); // lvmain_today.setchoicemode(listview.choice_mode_multiple); //this.listview=getl //listview = mainactivity.this.getlgetlistview(); lvmain_today.setitemscanfocus(false); lvmain_today.setchoicemode(listview.choice_mode_multiple); //arrayadapter<string> adapter = new arrayadapter<string> (this,android.r.layout.simple_list_item_multiple_choice, arr_date);// r.layout.item, my_list_item startmanagingcursor(c); int[] listfields = new int[] { r.id.txttitle, r.id.txtdatatasktoday }; string[] dbcolumns = new string[] { dbhelper.column_name, dbhelper.column_task }; log.d(tag, "трассировка" ); mainactivity.this.adapter = new mysqlcursoradapter( this, r.layout.my_list_item, c, dbcolumns, listfields, dbhelper); // lvmain_today.setadapter(mainactivity.this.adapter); // setlistadapter(mainactivity.this.adapter); names = arr_date; //c.close(); //db.close(); //dbhelper.close(); lvmain_today.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view view, int position, long id) { textview txtview = (textview) findviewbyid(r.id.txttitle); txtview.settext("blah blah blah"); log.d(tag, "position="+position); // ((textview) txtdatatasktoday).settextcolor(android.r.color.white); sparsebooleanarray chosen = ((listview) parent).getcheckeditempositions(); (int = 0; < chosen.size(); i++) { int key = chosen.keyat(i); if (chosen.get(key)) log.d(tag, "выделены ====="+names[key]); log.d(tag, "itemclick: position = " + position + ", id = " + id);} //****************nen пробная фигня************** // string[] columns = null; // string selection = null; // string[] selectionargs = null; // string groupby = null; // string having = null; // string orderby = null; // columns = new string[] {"name"}; // selection = "data_id = ?"; // selectionargs = new string[] {id_for_listtsk_today};//id_for_listtsk_today // cursor c = db.query("mytable", columns, selection, selectionargs, null, null, null); // string[] arr = logcursor(c); //************************************************** // string s=test(); } }); // lvmain_today.setonitemselectedlistener(new onitemselectedlistener() { // public void onitemselected(adapterview<?> parent, view view, // int position, long id) { // log.d(tag, "Было выделение позиции меню!!!!position = " + position + ", id = " // + id); // } // // public void onnothingselected(adapterview<?> parent) { // log.d(tag, "itemselect: nothing"); // } // }); } private string[] logcursor(cursor c) { // todo auto-generated method stub final string tag="states"; string[] arr_date = new string[c.getcount()];//string[] arr_date = new string[] {}; log.d(tag,"мы в курсоре"); if (c!=null) { if (c.movetofirst()) { // log.d(tag,"мы в курсоре1"); string str; int i=-1; { // log.d(tag,"мы в курсоре2"); str=""; i=i+1; (string cn: c.getcolumnnames()) { str = str.concat(c.getstring(c.getcolumnindex(cn))); } log.d(tag, "++++"+str); arr_date[i]=string.valueof(str); } while (c.movetonext()); } } return arr_date; } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. menu.add(0, 1, 0, "календарь"); menu.add(0, 2, 0, "Убрать выполненные"); menu.add(0, 3, 3, "Уйти"); // menu.add(1, 4, 1, "copy"); // menu.add(1, 5, 2, "paste"); // menu.add(1, 6, 4, "exit"); return super.oncreateoptionsmenu(menu); // getmenuinflater().inflate(r.menu.main, menu); //return true; } public boolean onoptionsitemselected(menuitem item) { // todo auto-generated method stub stringbuilder sb = new stringbuilder(); // Выведем в textview информацию о нажатом пункте меню // txtdatatasktoday.settext("item menu"); // txtdatatasktoday.settext(item.getgroupid()); txtdatatasktoday.settext("\r\n itemid: " + string.valueof(item.getitemid())); // txtdatatasktoday.settext("\r\n order: " + string.valueof(item.getorder())); // txtdatatasktoday.settext("\r\n title: " + item.gettitle()); switch (item.getitemid()) { case 1: intent intent = new intent(this, todocalendaractivity.class); startactivity(intent); ondestroy(); break; case 2: sparsebooleanarray sbarray = lvmain_today.getcheckeditempositions(); (int = 0; < sbarray.size(); i++) { int key = sbarray.keyat(i); if (sbarray.get(key)) log.d(tag, "выделены "+names[key]); spref = getpreferences(mode_private); editor ed = spref.edit(); ed.putstring(ten, "1"); ed.commit(); log.d(tag, "ставим константу для скрытия"); } break; case 3: spref = getpreferences(mode_private); string savedtext = spref.getstring(ten, ""); log.d(tag, "ten= "+ savedtext); ondestroy(); //finish(); break; } return super.onoptionsitemselected(item); } @override protected void onstart() { super.onstart(); try { mainactivity.this.onrestart(); } catch (exception e) { log.d(tag, "не получилось рестартануть"); } log.d(tag, "mainactivity: onstart()"); } @override protected void onresume() { super.onresume(); log.d(tag, "mainactivity: onresume()"); } protected void ondestroy() { super.ondestroy(); // закрываем подключение при выходе // todocalendaractivity.this.finish(); log.d(tag, "mainactivity: ondestroy()"); finish(); // db.close(); } @override protected void onpause() { super.onpause(); //this.dbhelper.close(); log.d(tag, "mainactivity: onpause()"); } @override protected void onstop() { super.onstop(); log.d(tag, "mainactivity: onstop()"); } @override protected void onrestart() { super.onrestart(); // new selectdatatask().execute(); log.d(tag, "mainactivity: onrestart()"); } // @override // public void onclick(view v) { // // todo auto-generated method stub // switch (v.getid()) { // case r.id.btnacttwo: // // intent intent = new intent(this, todocalendaractivity.class); // startactivity(intent); // break; // } // } }
mysqlcursoradapter
public class mysqlcursoradapter extends simplecursoradapter implements onclicklistener { final string tag="states"; private context context; private dbhelper dbhelper; private cursor currentcursor; textview txttitle; textview txttitle1; public mysqlcursoradapter(context context, int layout, cursor c, string[] from, int[] to, dbhelper dbhelper) { super(context, layout, c, from, to); log.d(tag, "трассировка1" ); this.currentcursor = c; this.context = context; this.dbhelper = dbhelper; log.d(tag, "mysqlcursoradapter()"); integer b = c.getcount(); log.d(tag, "b="+b); } public view getview(int pos, view inview, viewgroup parent) { log.d(tag, "getview() + posss=" + pos); view v = inview; if (v == null) { layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); v = inflater.inflate(r.layout.my_list_item, null); } this.currentcursor.movetoposition(pos); checkbox cbox = (checkbox) v.findviewbyid(r.id.bcheck); cbox.settag(integer.parseint(this.currentcursor.getstring(this.currentcursor.getcolumnindex(dbhelper.column_id)))); log.d(tag, "tag="+cbox.gettag().tostring()); if (this.currentcursor.getstring(this.currentcursor .getcolumnindex(dbhelper.column_status)) != null && integer.parseint(this.currentcursor .getstring(this.currentcursor .getcolumnindex(dbhelper.column_status))) != 0) { cbox.setchecked(true); } else { cbox.setchecked(false); } cbox.setonclicklistener(this); txttitle = (textview) v.findviewbyid(r.id.txttitle); txttitle1 = (textview) v.findviewbyid(r.id.txtdatatasktoday); txttitle.settext(this.currentcursor.getstring(this.currentcursor.getcolumnindex(dbhelper.column_name))); txttitle1.settext(this.currentcursor.getstring(this.currentcursor.getcolumnindex(dbhelper.column_task))); if (cbox.ischecked()) { log.d(tag, " pos=" + pos); txttitle.setpaintflags(txttitle.getpaintflags() | paint.strike_thru_text_flag); txttitle1.setpaintflags(txttitle.getpaintflags() | paint.strike_thru_text_flag); } return (v); } public void clearselections() { log.d(tag, "clearselections()"); this.dbhelper.clearselections(); this.currentcursor.requery(); } @override public void onclick(view v) { if(dbhelper.dbsqlite==null) { //log.d(tag, "00000000000"); sqlitedatabase db = dbhelper.getwritabledatabase(); //log.d(tag, "onclick"); checkbox cbox = (checkbox) v; integer _id = (integer) cbox.gettag(); //integer _id = 4; //log.d(tag, "integer _id="+_id.tostring()); contentvalues values = new contentvalues(); values.put(" status", cbox.ischecked() ? 1 : 0); try { db.update("mytable", values, "_id = ?", new string[] { integer.tostring(_id) }); } catch (sqlexception sqle) { // log.d(tag, "неудача"); throw sqle; } } txttitle.settextcolor(color.blue); } } }
i put flag in textview in functoin getview()
txttitle.setpaintflags(txttitle.getpaintflags() | paint.underline_text_flag);
txttitle1.setpaintflags(txttitle.getpaintflags() | paint.strike_thru_text_flag);
why first line crossed out?
setpaintflags
changes persistent, suspect function gets called twice, first time cbox.setchecked(true)
executed, second time cbox.setchecked(false)
executed, flags not reset. thus:
if (cbox.ischecked()) { log.d(tag, " pos=" + pos); txttitle.setpaintflags(txttitle.getpaintflags() | paint.strike_thru_text_flag); txttitle1.setpaintflags(txttitle.getpaintflags() | paint.strike_thru_text_flag); } else { log.d(tag, "!pos=" + pos); txttitle.setpaintflags(txttitle.getpaintflags() & ~paint.strike_thru_text_flag); txttitle1.setpaintflags(txttitle.getpaintflags() & ~paint.strike_thru_text_flag); }
Comments
Post a Comment