本文介绍了在ContentValues​​阵列复制ContentValues的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以用这个任何帮助,确保它是简单的东西,但不能看到它。

Can any help with this, sure it is something simple but can't see it.

做一个bulkInsert到内容提供商(UserDictionary),但所有的刀片具有相同的字值。问题是ContentValues​​的数组。
这是一些测试code我有:

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());


    }

和从日志中,可以看到有mValueArray重复的值。

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

推荐答案

您正在修改的同一对象。这应该很好地工作:

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());


    }

关于为什么你的code不起作用更多的解释了一下:当你使用的添加()的,你保存对象的参考(指针)在列表的。它不会复制的对象。当您修改同一个对象后,在参考列表的仍然指向这一个,这样的话你正在修改一个为好。你只需要为同一对象2引用。

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.

这篇关于在ContentValues​​阵列复制ContentValues的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 20:49