Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中使用where子句按类别计算行数_Android_Sqlite - Fatal编程技术网

在android中使用where子句按类别计算行数

在android中使用where子句按类别计算行数,android,sqlite,Android,Sqlite,我正在使用自定义游标适配器从SQlite视图中获取listview中的类别,并且还希望显示使用相同类别名称保存的特定行数。我尝试了很多次,但不幸的是没有成功 这里是我使用的查询 int GetTasksCountByCategory(String cat) { Cursor cur = mDb.rawQuery(" SELECT Count(*) FROM ViewTasks where Category_name = "+"'"+"cat"+"'", null); i

我正在使用自定义游标适配器从SQlite视图中获取listview中的类别,并且还希望显示使用相同类别名称保存的特定行数。我尝试了很多次,但不幸的是没有成功

这里是我使用的查询

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;    
 }
还有我的适配器类

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);
    }

}
在这个例子中,我得到了空指针异常 有人能帮我吗
thanx提前

两件事。首先,当mContext有一个值时,应该在类的构造函数中完成
DbAdapter myAdapter
的赋值

public cursorAdapter2(Context context, Cursor c) {
    super(context, c);
    mContext=context;
    myAdapter = new DbAdapter(mContext);
}
其次,修改来自…的查询

Cursor cur = mDb.rawQuery(" SELECT Count(*) FROM  ViewTasks where Category_name = "+"'"+"cat"+"'", null);


您正在传递字符串
“cat”
而不是变量
cat

添加logcat应该很好。先生,我无法加载logcat img,因为我的声誉低于10。您可以编辑您的帖子并指出NPE发生在哪一行吗?当我在我的包名上单击logcat时,这一行的错误显示为crsor cur=mDb.rawQuery(“从ViewTasks中选择Count(*),其中Category_name=“+”“+”cat“+”,null);太好了!如果您能接受我的回答,我将不胜感激。祝您今后好运。”。
Cursor cur = mDb.rawQuery("SELECT Count(*) FROM ViewTasks where Category_name = ?", new String[] { cat } );