对此有任何帮助,确定它是简单的但看不到它.
对内容提供程序(UserDictionary)执行bulkInsert,但所有插入都具有相同的“单词”值.问题是Array of ContentValues.
这是我的一些测试代码:
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()); }
从日志中可以看出mValueArray有重复的值.
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=en02-22 12:33:51.070: I/log(859): appID=dictionary word=WORD2 frequency=255 locale=en
显然我在向数组添加值时做了一些不正确的事情.谁能帮我?
谢谢
解决方法:
你正在修改同一个对象.这应该工作正常:
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()); }
关于为什么你的代码不起作用的一些更多解释:当你使用add()时,你将在List中保存对象引用(指针).它不会复制对象.稍后修改同一个对象时,List中的引用仍然指向此引用,因此您也要修改该引用.您只有2个引用同一个对象.
总结以上是内存溢出为你收集整理的android – ContentValues数组中的重复ContentValues全部内容,希望文章能够帮你解决android – ContentValues数组中的重复ContentValues所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)