sqlite - Count rows by category using where clause in android -
i using custom cursor adapter catagories in listview sqlite view , want show specific rows count saved same category name.i seek many times unfortuntely can't succeed.
here query used
int gettaskscountbycategory(string cat) { cursor cur = mdb.rawquery(" select count(*) viewtasks category_name = "+"'"+"cat"+"'", null); int x = 0; if (cur.movetofirst()) { x = cur.getint(0); } cur.close(); homecoming x; }
and adapter class
public class cursoradapter2 extends cursoradapter{ private context mcontext; dbadapter myadapter = new dbadapter(mcontext); @suppresswarnings("deprecation") public cursoradapter2(context context, cursor c) { super(context, c); mcontext=context; // todo auto-generated constructor stub } @override public view newview(context arg0, cursor arg1, viewgroup arg2) { // todo auto-generated method stub layoutinflater inflater = layoutinflater.from(arg2.getcontext()); view retview = inflater.inflate(r.layout.list, arg2, false); homecoming retview; } @suppresswarnings("unused") @override public void bindview(view view, context arg1, cursor cursor) { // todo auto-generated method stub textview category = (textview) view.findviewbyid(r.id.txtlastcatid); category.settext(cursor.getstring(cursor.getcolumnindex(dbadapter.key_category_name))); string getcat = (string) category.gettext(); int = myadapter.gettaskscountbycategory(getcat); button count = (button) view.findviewbyid(r.id.button2); count.settext(integer.tostring(i)); button img = (button) view.findviewbyid(r.id.button1); } }
in null pointer exception plz help me thanx in advance
two things. first assignment of dbadapter myadapter
should done in class' constructor...when mcontext has value.
public cursoradapter2(context context, cursor c) { super(context, c); mcontext=context; myadapter = new dbadapter(mcontext); }
secondly modify query from....
cursor cur = mdb.rawquery(" select count(*) viewtasks category_name = "+"'"+"cat"+"'", null);
to
cursor cur = mdb.rawquery("select count(*) viewtasks category_name = ?", new string[] { cat } );
you passing in string "cat"
not variable cat
.
android sqlite
No comments:
Post a Comment