最近在项目中需要使用COUNT()函数搜索相同的项,使用SQL语句是这样的
SELECT id, COUNT(*) as count FROM table WHERE item != '' GROUP BY item ORDER BY count DESC
public int getCount(String param1, String param2){ SQLiteDatabase db = this.getReadableDatabase(); long count; count = DatabaseUtils.queryNumEntries(db, "TABLE_NAME", "param1=? AND param2=?", new String[]{param1, param2}); db.close(); return count; }
换做contentprovider方式如何实现呢?
参考:http://stackoverflow.com/questions/16408007/count-on-custom-contentprovider
Table
public static class Promotion implements BaseColumns { private Promotion() { } public static final String TABLE_NAME = "promotions"; public static final String PATH_PROMOTIONS = "/promotions"; public static final String PATH_PROMOTION_ID = "/promotion/#"; public static final Uri CONTENT_URI = Uri.parse(SCHEME + AUTHORITY + PATH_PROMOTIONS); public static final Uri CONTENT_URI_ID = Uri.parse(SCHEME + AUTHORITY + PATH_PROMOTION_ID); public static final String COLUMN_NAME_ADVERTISER = "advertiser"; public static final String COLUMN_NAME_SUBJECT = "subject"; public static final String COLUMN_NAME_URL = "promo_url"; public static final String COLUMN_NAME_CATEGORY = "category"; public static final String COLUMN_NAME_READED = "is_readed"; public static final String COLUMN_NAME_STARRED = "is_starred"; public static final String COLUMN_NAME_UNTIL = "until"; public static final String COLUMN_NAME_CREATE_DATE = "created"; public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.package.promotion"; public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.package.promotion"; }
SELECT COLUMN_NAME_ADVERTISER, COUNT(COLUMN_NAME_ADVERTISER) AS _COUNT FROM TABLE_NAME WHERE (COLUMN_NAME_READED = ?) GROUP BY COLUMN_NAME_ADVERTISER;
public class InboxListFragment extends ListFragment { private SimpleCursorAdapter mAdapter; @Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_list, container, false); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); fillData(); } private void fillData() { final String[] from = { MyPackage.Promotion.COLUMN_NAME_ADVERTISER, MyPackage.Promotion._COUNT }; final int[] to = new int[] { android.R.id.text1 }; mAdapter = new SimpleCursorAdapter(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, null, from, to, 0); setListAdapter(mAdapter); getLoaderManager().initLoader(0, null, new myLoader()); } public class myLoader implements LoaderCallbacks<Cursor> { @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { final String count = String.format("COUNT(%s) as %s", MyPackage.Promotion.COLUMN_NAME_ADVERTISER, MyPackage.Promotion._COUNT); final String[] projection = { MyPackage.Promotion.COLUMN_NAME_ADVERTISER, count }; final String selection = MyPackage.Promotion.COLUMN_NAME_READED + " = ?"; final String[] selectionArgs = { "0" }; final CursorLoader loader = new CursorLoader(getActivity(), MyPackage.Promotion.CONTENT_URI, projection, selection, selectionArgs, null); return loader; } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { mAdapter.swapCursor(cursor); } @Override public void onLoaderReset(Loader<Cursor> loader) { mAdapter.swapCursor(null); } } }