有人能给我解释一下吗?
这是我的代码,然后,我要显示日志。事实上我有一个物品清单。我记下它的尺寸: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

最佳答案

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

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

tripstepspsTrip.getTripSteps()指向同一个对象,因此当您调用psTrip.getTripSteps().clear();时,也会清除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;
}

您需要在tripsteps类中创建一个构造函数,它可以复制另一个实例:
class TripStep
{
    public TripStep()
    { ... } // Regular constructor

    public TripStep(TripStep cloneFrom) {
        // Copy all the fields of TripStep.
    }
}

10-08 16:30