The following code gives all values from database even duplicate values.
return database.query("contacts", new String[] {"_id", "name"},
null, null, null, null, "name");
how can i get distinct values from above code
Use the overload of query that takes a boolean as first parameter and remove the _id column from the select:
return database.query(true, "contacts", new String[] {"name"},
null, null, null, null, "name", null);
The _id column is supposed to be unique, so including it in your query will lead to the distinct having no effect.
use this query:
public Cursor query (boolean distinct, String table,
String[] columns, String selection,
String[] selectionArgs, String groupBy,
String having, String orderBy, String limit)
set the first param to true and do not use youre primarey key in selection. it will be always unique...
rawQueryinstead ofqueryand use a real SQL query with theDISTINCTkeyword to perform this action. See also a similar question here.queryas indicated by Stefan's or my answer?