1

有人可以向我解释一下吗?这是我的代码,之后,我将显示日志。事实是我有一个对象列表。我记录它的大小:1。我清除它,变成 0。我在其中添加对象,它仍然是 0。

这是我的代码:

 Log.i("", "update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE" + index);
                    RealmList<TripStep> tripsteps = psTrip.getTripSteps();
                    Log.i("", "update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE2:" + psTrip.getTripSteps().size());
                    realmActive.beginTransaction();
                    TripStep tripStep = realmActive.copyToRealmOrUpdate(updateStepResponse.getStep());
                    realmActive.commitTransaction();
                    Log.i("", "update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE3 + index is:" + index);
                    realmActive.beginTransaction();
                    tripsteps.set(index, tripStep);
                    realmActive.commitTransaction();
                    Log.i("", "update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE4");
                    if (isRoaming) {
                        if ((tripsteps.size() - 2 == index) && !Utils.hasStarted(PSNewJourneyActivity.this)) {
                            realmActive.beginTransaction();
                            tripsteps.get(tripsteps.size() - 1).setTravelMode(updateStepResponse.getStep().getTravelMode());
                            realmActive.commitTransaction();
                        }
                    }
                    Log.i("", "update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE5 tripsteps not in pstrip:" + tripSteps.size());
                    Log.i("", "update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE5 tripsteps not in pstrip before:" + psTrip.getTripSteps().size());
                    realmActive.beginTransaction();
                    psTrip.getTripSteps().clear();
                    realmActive.commitTransaction();
                    Log.i("", "update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE5 tripsteps not in pstrip AFTER CLEAR:" + psTrip.getTripSteps().size());
                    realmActive.beginTransaction();
                    for (TripStep tripStep1 : tripsteps){
                        psTrip.getTripSteps().add(tripStep1);
                    }
                    realmActive.commitTransaction();
                    realmActive.beginTransaction();
                    realmActive.copyToRealmOrUpdate(psTrip);
                    realmActive.commitTransaction();
                    Log.i("", "update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE6 tripstep size:" + psTrip.getTripSteps().size());
                    Handler han = new Handler();
                    han.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            continueInit(true, true);
                            ProgressForMap(true);
                        }
                    }, mapDelay);

这是我的日志:

update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE0
09-09 10:12:11.219  32740-32740/nl.hgrams.passenger I/﹕ update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE2:1
09-09 10:12:11.251  32740-32740/nl.hgrams.passenger I/﹕ update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE3 + index is:0
09-09 10:12:11.257  32740-32740/nl.hgrams.passenger I/﹕ update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE4
09-09 10:12:11.257  32740-32740/nl.hgrams.passenger I/﹕ update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE5 tripsteps not in pstrip:1
09-09 10:12:11.257  32740-32740/nl.hgrams.passenger I/﹕ update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE5 tripsteps not in pstrip before:1
09-09 10:12:11.264  32740-32740/nl.hgrams.passenger I/﹕ update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE5 tripsteps not in pstrip AFTER CLEAR:0
09-09 10:12:11.277  32740-32740/nl.hgrams.passenger I/﹕ update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE6 tripstep size:0
4

1 回答 1

2

这有点令人费解。我要说的一件事是:

                    // this part makes sense
                    realmActive.beginTransaction(); 
                    for (TripStep tripStep1 : tripsteps){
                        psTrip.getTripSteps().add(tripStep1);
                    }
                    realmActive.commitTransaction();

                    // why are you then doing this?
                    //realmActive.beginTransaction();
                    //realmActive.copyToRealmOrUpdate(psTrip);
                    //realmActive.commitTransaction();

据我所知,仅第一部分就足够了,第二部分什么都不做。

更新:

刚刚注意到您的变量tripsteps是在这里分配的:

RealmList<TripStep> tripsteps = psTrip.getTripSteps();

然后稍后你会这样做:

psTrip.getTripSteps().clear();

tripsteps并且psTrip.getTripSteps()指向同一个对象,所以当你打电话时psTrip.getTripSteps().clear();你也在清理tripsteps

因此,此时将没有任何内容tripsteps

for (TripStep tripStep1 : tripsteps){ // tripsteps.size() == 0
    psTrip.getTripSteps().add(tripStep1);
}

我认为你需要“深拷贝”你的数组列表:

这是错误的:

RealmList<TripStep> tripsteps = psTrip.getTripSteps();

改为这样做:

RealmList<TripStep> tripsteps = cloneList(psTrip.getTripSteps());

public List<TripStep> cloneList(RealmList<TripStep> list) {
    List<TripStep> clone = new ArrayList<TripStep>(list.size());
    for(TripStep item : list){
        clone.add(new TripStep(item));
    }

    return clone;
}

您需要在您的TripStep类中创建一个可以从另一个实例复制的构造函数:

class TripStep
{
    public TripStep()
    { ... } // Regular constructor

    public TripStep(TripStep cloneFrom) {
        // Copy all the fields of TripStep.
    }
}
于 2015-09-09T08:41:46.643 回答