Reputation: 139
Can any help with this, sure it is something simple but can't see it.
Doing a bulkInsert to content provider (UserDictionary) but all inserts have same "word" value. Issue is Array of ContentValues. This is some test code I have:
public void mClick(View v){
int batchSize = 25;
ContentValues[] mValueArray = new ContentValues[batchSize];
List<ContentValues>mValueList = new ArrayList<ContentValues>();
ContentValues mNewValues = new ContentValues();
mNewValues.put(UserDictionary.Words.APP_ID, this.getPackageName());
mNewValues.put(UserDictionary.Words.LOCALE, "en");
mNewValues.put(UserDictionary.Words.FREQUENCY, "255");
mNewValues.put(UserDictionary.Words.WORD, "WORD1");
mValueList.add(mNewValues);
mNewValues.put(UserDictionary.Words.APP_ID, this.getPackageName());
mNewValues.put(UserDictionary.Words.LOCALE, "en");
mNewValues.put(UserDictionary.Words.FREQUENCY, "255");
mNewValues.put(UserDictionary.Words.WORD, "WORD2");
mValueList.add(mNewValues);
mValueArray = new ContentValues[mValueList.size()];
mValueList.toArray(mValueArray);
Log.i(TAG,mValueList.toString());
Log.i(TAG,mValueArray[0].toString());
Log.i(TAG,mValueArray[1].toString());
}
and from log, can see that mValueArray has duplicate values.
02-22 12:33:51.060: I/log(859): [appid=dictionary word=WORD2 frequency=255 locale=en, appid=dictionary word=WORD2 frequency=255 locale=en]
02-22 12:33:51.070: I/log(859): appid=dictionary word=WORD2 frequency=255 locale=en
02-22 12:33:51.070: I/log(859): appid=dictionary word=WORD2 frequency=255 locale=en
Obviously I am doing something incorrect with adding values to arrays. Can any one help me? Thanks
Upvotes: 3
Views: 3424
Reputation: 25873
You're modifying the same object. This should work fine:
public void mClick(View v){
int batchSize = 25;
ContentValues[] mValueArray = new ContentValues[batchSize];
List<ContentValues>mValueList = new ArrayList<ContentValues>();
ContentValues mNewValues = new ContentValues();
mNewValues.put(UserDictionary.Words.APP_ID, this.getPackageName());
mNewValues.put(UserDictionary.Words.LOCALE, "en");
mNewValues.put(UserDictionary.Words.FREQUENCY, "255");
mNewValues.put(UserDictionary.Words.WORD, "WORD1");
mValueList.add(mNewValues);
mNewValues = new ContentValues();
mNewValues.put(UserDictionary.Words.APP_ID, this.getPackageName());
mNewValues.put(UserDictionary.Words.LOCALE, "en");
mNewValues.put(UserDictionary.Words.FREQUENCY, "255");
mNewValues.put(UserDictionary.Words.WORD, "WORD2");
mValueList.add(mNewValues);
mValueArray = new ContentValues[mValueList.size()];
mValueList.toArray(mValueArray);
Log.i(TAG,mValueList.toString());
Log.i(TAG,mValueArray[0].toString());
Log.i(TAG,mValueArray[1].toString());
}
A bit of more explanation about why your code does not work: when you use add(), you're saving the object reference (pointer) in the List. It does not copy the object. When you modify that same object later, the reference in the List still points to this one, and thus you're modifying that one as well. You just have 2 references for the same object.
Upvotes: 8
Reputation: 183321
The problem is that you just have a single ContentValues
object:
ContentValues mNewValues = new ContentValues();
and you add it to the list twice. This line:
mNewValues.put(UserDictionary.Words.WORD, "WORD2");
modifies that object; even though it comes after the first time you've added the object to the list, you still see it in the list, because it's still the same object.
To fix this, follow this:
mValueList.add(mNewValues);
with this:
mNewValues = new ContentValues();
to get a new instance to add.
Upvotes: 4