How do I add contact to a group on Android? - android

I read about
ContactsContract.CommonDataKinds.GroupMembership, but I can't figure out what URI use to insert to.
I have prepared this method:
public static Uri addToGroup(ContentResolver resolver, long personId,
long groupId) {
ContentValues values = new ContentValues();
values.put(ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID, personId);
values.put(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, groupId);
return resolver.insert(uri, values);//URI is not known
}
Can someone tell me what URI to use in SDK 2.0+?

I found the resolution and I post it here:
public Uri addToGroup(long personId, long groupId) {
//remove if exists
this.removeFromGroup(personId, groupId);
ContentValues values = new ContentValues();
values.put(ContactsContract.CommonDataKinds.GroupMembership.RAW_CONTACT_ID,
personId);
values.put(
ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID,
groupId);
values
.put(
ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE,
ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE);
return this.ctx.getContentResolver().insert(
ContactsContract.Data.CONTENT_URI, values);
}
But I don't get something, why do I have to use RAW_CONTACT_ID and not CONTACT_ID, the later raises nullpointerexception.

Related

update column with different values sqlite database android studio

I have a hidden database with 7 columns, one of which is a price and whose values ​​are null. I want to enter some value in the field column for id = 1, then for id = 2 I enter another value etc. and when I fill in the price column I want to update the whole. Is that possible?
My code updates only one value
public boolean updateData(String id,String name, String number_phone, String about, Integer price, String color){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, id);
contentValues.put(COL_2, name);
contentValues.put(COL_3, number_phone);
contentValues.put(COL_4, about);
contentValues.put(COL_5, price);
contentValues.put(COL_6, color);
db.update(TABLE_NAME, contentValues, " ID = ?" , new String[]{id});
return true;
}
I don't quite get your question but from what i understand this could might help you out.
If you want only one field values to be updated then you need to use for loop.
In onCreate method call this with the array of prices that you want to be updated in table's each row.
Array of prices should not be greater than no. of rows exist in table.
for(int i=0;i<(no. of rows in table);i++){
updateData(rowId,priceArray[i]);
}
Now the update method will be like.
public boolean updateData(String id, Integer price){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_5, price);
db.update(TABLE_NAME, contentValues, " ID = ?" , new String[]{id});
return true;
}
If you want to update certain rows for example {1,4,5} then you have to update it one by one by calling updateData method with id and prices .
But if you want same value of price field to be updated in certain rows like above {1,4,5} than you can
public boolean updateData(Integer price){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_5, price);
db.update(TABLE_NAME, contentValues, " ID = ?" , new String[]{1,4,5});
return true;
}

How to Insert or Update table On base of Name value in Sqlite android

public long createCountry(String code, String name, String continent,
String region) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_PRODUCT_TYPE, code);
initialValues.put(KEY_PRODUCT_NAME, name);
initialValues.put(KEY_PRODUCT_QUANTITY, continent);
initialValues.put(KEY_PRODCUT_COST, region);
return mDb.insertWithOnConflict(SQLITE_TABLE, null, initialValues, SQLiteDatabase.CONFLICT_REPLACE);
}
this is for insert or Update function but its taking new Row if name X is alredy present in table row i want it should Update Like suppose we have 3 Entry whose name column value has X,Y,Z now Suppose i want call again this function with name X then rest of value Should update not its should create another Column please help me where am doing wrong .
There is no update-or-insert function.
You have to do this manually:
public void createCountry(String code, String name, String continent, String region) {
ContentValues values = new ContentValues();
values.put(KEY_PRODUCT_TYPE, code);
values.put(KEY_PRODUCT_QUANTITY, continent);
values.put(KEY_PRODCUT_COST, region);
int updated = mDb.update(SQLITE_TABLE, values,
KEY_PRODUCT_NAME + " = ?",
new String[]{ name });
if (updated == 0) {
values.put(KEY_PRODUCT_NAME, name);
mDb.insert(SQLITE_TABLE, null, values);
}
}

how to write or insert function and update in sq-lite in android

public void insertData(String cateogry, String status, String feedback,
String date, Bitmap bitmap) {
db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(CATEGORY, cateogry);
cv.put(STATUS, status);
cv.put(FEEDBACK, feedback);
cv.put(IMAGE, Utility.getBytes(bitmap));
cv.put(DATE, date);
db.insert(INFR_TABLE, CATEGORY, cv);
}
public void updateData(String cateogry, String status, String feedback,
String date, Bitmap bitmap)
{
db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(CATEGORY, cateogry);
cv.put(STATUS, status);
cv.put(FEEDBACK, feedback);
cv.put(IMAGE, Utility.getBytes(bitmap));
cv.put(DATE, date);
db.update(INFR_TABLE, cv, "cateogry="+cateogry, null);
}
this is update and insert funcation in sqlite i want when row of cateogry is alredy there then update it with other value if not then insert . and i am calling like this way :
for(table)
String data[] = dh.getAllCategory();
if (Arrays.asList(data).contains("Table")) {
dh.updateData("Table", t, j, "25.5.2014", BitmapFactory
.decodeResource(getResources(), R.drawable.ic_launcher));
}
else {
dh.insertData("Table", t, j, "25.5.2014", BitmapFactory
.decodeResource(getResources(), R.drawable.ic_launcher));
}
for(water) :
String data[] = dh.getAllCategory();
// dh.deleteitem(11);
if (Arrays.asList(data).contains("Table")) {
dh.updateData("Water", t, j, "25.5.2014", BitmapFactory
.decodeResource(getResources(), R.drawable.ic_launcher));
}
else
{
dh.insertData("Water", t, j, "25.5.2014", BitmapFactory
.decodeResource(getResources(), R.drawable.ic_launcher));
}
but every time its create new row and duplicate please help me where am doing wrong we have 2 class in on is for table and another for water. please tell
public void insertData(String id, String cateogry, String status,
String feedback, String date, Bitmap bitmap) {
db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(ID, id);
cv.put(CATEGORY, cateogry);
cv.put(STATUS, status);
cv.put(FEEDBACK, feedback);
cv.put(IMAGE, Utility.getBytes(bitmap));
cv.put(DATE, date);
db.insertWithOnConflict(INFR_TABLE, ID, cv,
SQLiteDatabase.CONFLICT_REPLACE);
}
try this and Update i think it should work ..
Instead of maintaining to two blocks of code you can maintain into a single block by using insertWithOnConflict.
public long insertWithOnConflict (String table, String nullColumnHack,
ContentValues initialValues, int conflictAlgorithm)
In the conflictAlgorithm you can specify what if an conflict occurs while inserting the record here you can specify CONFLICT_REPLACE
According to docs,
When a UNIQUE constraint violation occurs, the pre-existing rows that
are causing the constraint violation are removed prior to inserting or
updating the current row. Thus the insert or update always occurs. The
command continues executing normally. No error is returned.
so simply you can specify as
database.insertWithOnConflict(tablename, columnHack, values, SQLiteDatabase.CONFLICT_REPLACE);
it will inserts the record if it already exists otherwise it will update.

Updating a data base with a single field

I'm currently developing an Android app. The database has 1 column only named content. Here it is the code:
public long insert(String content){
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_CONTENT, content);
return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}
How to make an update for a specific column (according to its index) in that database?
db.update
(
MYDATABASE_TABLE, contentValues , ID_FIELD_CONST + " = ?",
new String[] { idValue }
);
You can use:
ContentValues cv = new ContentValues();
cv.put(KEY1,value1);
cv.put(KEY2,value2);
//... all the fields you want to update
String where = KEY_CONTENT + " = " + value;
db.update(table_name,cv,where,null);
KEY1,KEY2 ... are the name of the fields you want to update
value1,value2... are the new values of the fields you want to update
the where String is to tell in which row you need to update.

New Contact not listed

I used this code to add the contact:
public static long addNewNameToContact(Context context, String name) {
long rawContactId = 0;
ContentValues values = new ContentValues();
values.put(Contacts.DISPLAY_NAME, name);
Uri rawContactUri = context.getContentResolver().insert(RawContacts.CONTENT_URI, values);
rawContactId = ContentUris.parseId(rawContactUri);
return rawContactId;
}
(I need to create a contact only with the name and then add phone and other data).
Can you help me?
Very thanks,
Mateus
First, you need to create a raw contact specifying the account type and name. The account type and name can be any string. For instance accountType="com.mateus.app" and accountName="user":
ContentValues values = new ContentValues();
values.put(RawContacts.ACCOUNT_TYPE, accountType);
values.put(RawContacts.ACCOUNT_NAME, accountName);
Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
Then, you can set the display name with the raw contact ID that is returned above:
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(StructuredName.DISPLAY_NAME, "Mike Sullivan");
getContentResolver().insert(Data.CONTENT_URI, values);

Categories

Resources