-2

我创建了一种将请求 SQLite 的结果存储在表中的方法

public ArrayList <String> getProductName(double idcat) 
    {
        ArrayList <String> tab = new ArrayList <String>();
         try
            {
                Cursor c = null;
                c = database.rawQuery("SELECT " + COL_PRODUCT_NAME + " FROM "
                        + TABLE_PRODUCT + " WHERE " + COL_CATEGORY + "=  '" + idcat + "'", null);

                for(int i=0;i<c.getCount();i++)
                {
                    tab.add(c.getString(c.getColumnIndex(COL_PRODUCT_NAME)));
                    c.moveToNext();
                  }
                c.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

                return tab;
        }

我想得到这个表中元素的数量,所以我把这个:

ArrayList <String> tab = new ArrayList <String>();
 TextView tv = new TextView(this);
                tab=db.getProductName(1);
                int n =tab.size();
                tv.setText("n=" +n);

但是当我编译我的应用程序时,n 得到 0 !!! 我想知道我的方法是否正确

4

1 回答 1

1

我认为您需要在开始 Cursor 迭代之前先进行此调用:

c.moveToFirst()

它应该是这样的

if(c.moveToFirst()){
 //Loop here
}
于 2013-07-19T18:16:43.210 回答