i am using custom cursor adapter to get catagories in listview from SQlite view and also want to show specific rows count saved with same category name.i try many times but unfortuntely can’t succeed.
here the Query which i used
int GetTasksCountByCategory(String cat)
{
Cursor cur = mDb.rawQuery(" SELECT Count(*) FROM ViewTasks where Category_name = "+"'"+"cat"+"'", null);
int x = 0;
if (cur.moveToFirst())
{
x = cur.getInt(0);
}
cur.close();
return x;
}
and my 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);
return 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 i = 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 this i get null pointer exception
anyone plz help me
thanx in advance
two things. first the assignment of
DbAdapter myAdaptershould be done in the class’ constructor…when mContext has a value.secondly modify the query from….
to
you are passing in the string
"cat"not the variablecat.