android - Why getView not getting called? -
i have adapter extends simplecursoradapter. reason can't seem see, getview not being called. have breakpoint inside getview , never gets there , list shows empty. can take thru , see i've done wrong?
package com.example.ok1; import java.text.simpledateformat; import java.util.date; import android.app.activity; import android.content.contentvalues; import android.content.intent; import android.content.sharedpreferences; import android.content.sharedpreferences.editor; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.os.bundle; import android.util.log; import android.util.sparsebooleanarray; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.listview; import android.widget.textview; import android.app.listactivity; 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; //public static string id_for_listtsk_today; // static sqlitedatabase db; mysqlcursoradapter adapter = null; //***********************************************8 @override protected void oncreate(bundle savedinstancestate) { 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(id_for_listtsk_today.tostring()); 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(); } 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 }; string[] dbcolumns = new string[] { dbhelper.column_name }; 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) { 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); 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); finish(); break; } return super.onoptionsitemselected(item); } // @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
package com.example.ok1; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.checkbox; import android.widget.simplecursoradapter; import android.widget.textview; public class mysqlcursoradapter extends simplecursoradapter implements onclicklistener { final string tag="states"; private context context; private dbhelper dbhelper; private cursor currentcursor; 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); textview txttitle = (textview) v.findviewbyid(r.id.txttitle); txttitle.settext(this.currentcursor.getstring(this.currentcursor .getcolumnindex(dbhelper.column_name))); return (v); } public void clearselections() { log.d(tag, "clearselections()"); //this.dbhelper.clearselections(); //this.currentcursor.requery(); } @override public void onclick(view v) { log.d(tag, "onclick"); checkbox cbox = (checkbox) v; integer _id = (integer) cbox.gettag(); log.d(tag, "integer _id="+_id.tostring()); contentvalues values = new contentvalues(); values.put(" selected", cbox.ischecked() ? 1 : 0); //this.dbhelper.dbsqlite.update(sqlhelper.table_name, values, "_id=?", // new string[] { integer.tostring(_id) }); } }
dbhelper
package com.example.ok1; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.database.sqlite.sqlitequerybuilder; import android.util.log; public class dbhelper extends sqliteopenhelper { final string tag="states"; public static final string column_id = "_id"; public static final string column_data = "data_id"; public static final string column_name = "name"; public static final string column_status = "status"; public static final string table_name = "mytable"; public sqlitedatabase dbsqlite; public dbhelper(context context) { // конструктор суперкласса super(context, "mydb", null, 1); } @override public void oncreate(sqlitedatabase db) { log.d(tag, "--- oncreate database ---"); // создаем таблицу с полями db.execsql("create table mytable (" + "_id integer primary key autoincrement," + "data_id text," + "name text," + "task text," + "status integer" + ");"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } public cursor getcursor() { log.d(tag, "getcursor() получили курсор с базы"); string[] columns = null; string selection = null; string[] selectionargs = null; string groupby = null; string having = null; string orderby = null; // sqlitequerybuilder querybuilder = new sqlitequerybuilder(); // querybuilder.settables(table_name); // string[] ascolumnstoreturn = new string[] { column_id, column_name, // column_data, column_status }; // cursor mcursor = querybuilder.query(dbsqlite, ascolumnstoreturn, null, // null, null, null, "title asc"); // log.d(tag, "getcursor() получили курсор с базы конец"); final sqlitedatabase db = this.getwritabledatabase(); columns = new string[] { column_id, column_data, column_name, column_status }; // selection = "data_id = ?"; // selectionargs = new string[] {id_for_listtsk_today}; cursor c = db.query("mytable", columns, null, null, null, null, null); return c; } }
activitymain.xml
<!--?xml version="1.0" encoding="utf-8"?--> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#81bef7" android:scrollbars="vertical"> <tablelayout android:id="@+id/tablelayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" > <tablerow> <listview android:id="@+id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" > </listview> </tablerow> </tablelayout> </linearlayout>
my_list_item.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" > <checkbox android:id="@+id/bcheck" android:layout_height="wrap_content" android:layout_width="wrap_content" android:focusable="false"> </checkbox> <textview android:id="@+id/txttitle" android:layout_width="138dp" android:layout_height="?android:attr/listpreferreditemheight" android:focusable="false" android:gravity="left|center_vertical" android:text="test" > </textview> <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right|center_vertical" android:orientation="horizontal" > <imagebutton android:id="@+id/imagebutton01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.50" android:clickable="true" > </imagebutton> </linearlayout> </linearlayout>
i removed
c.close(); db.close();
and working fine.
Comments
Post a Comment