0

The main goal is to make a listVeiw with chronometers and you turn the screen to save the state of each timer. But when I run the chronometer and turn the screen there is no matter from which timer always first item. It wasn't formally launched, but the count began. Here is fragment of code:

if(row==null){
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(resorceID,parent,false);
        holder = new TrackHolder();
        holder.name = (TextView)row.findViewById(R.id.row_name);
        holder.chronometer = (Chronometer)row.findViewById(R.id.row_chronometer);
        holder.start = (Button)row.findViewById(R.id.btStart);
        holder.stop = (Button)row.findViewById(R.id.btStop);
        row.setTag(holder);
    }else{
        holder = (TrackHolder)row.getTag();
    }
    if(!trackerList.isEmpty()) {

        final TrackHolder finalHolder = holder;
        holder.start.setEnabled(true);
        holder.stop.setEnabled(false);

        if(bundle!=null){

            holder.name.setText(tracker.getName());
            elapsedTime = bundle.getLong("elapsedTime "+position);//получеаем значение прошедшего времени после поворота экрана
            base = bundle.getLong("base " + position);//получаем, сохраненную на пред экране.
            lastPause[0]=bundle.getLong("lastPause " + position);//получаем разницу во времени(паузу) с SavedInstanceState
            bundleIsItStart=bundle.getBoolean("start " + position);//получаем статус: запущен/остановлен
            basesList.set(position,base);

            if(elapsed.get(position)<elapsedTime)//если время, полученое после поворота больше, которое насчитает в onTick, запишется в список, но такого никогда не будет
                elapsed.set(position, elapsedTime);
            if(startList.get(position)==bundleIsItStart)//по аналогии как выше, если если текущий элемент == статусу, пишем в список статус, иначе элемент остается в списке
                startList.set(position,bundleIsItStart);
            if(lastPauseList.get(position)>lastPause[0])
                lastPauseList.set(position,lastPause[0]);

            if(bundleIsItStart){
                holder.stop.setEnabled(true);
                holder.start.setEnabled(false);
                holder.chronometer.setBase(base);
                holder.chronometer.start();
            }


        }else{

            holder.name.setText(tracker.getName());
            holder.start.setEnabled(true);
            holder.stop.setEnabled(false);
        }
        holder.start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finalHolder.chronometer.setBase(SystemClock.elapsedRealtime() + lastPause[0]);
                finalHolder.chronometer.start();
                finalHolder.stop.setEnabled(true);
                finalHolder.start.setEnabled(false);
                basesList.set(position, finalHolder.chronometer.getBase());
                startList.set(position,true);
            }
        });
        holder.stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lastPause[0]=finalHolder.chronometer.getBase()-SystemClock.elapsedRealtime();
                finalHolder.chronometer.stop();
                finalHolder.start.setEnabled(true);
                finalHolder.stop.setEnabled(false);
                lastPauseList.set(position,lastPause[0]);
                startList.set(position,false);
            }
        });
        holder.chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
            @Override
            public void onChronometerTick(Chronometer chronometer) {
                    elapsed.set(position, SystemClock.elapsedRealtime() - finalHolder.chronometer.getBase());
                    Log.d("myTag", "elapsedTime = " + getTime(elapsed.get(position)) + "position = " + position);

            }
        });
    }
    return row;
}

I debuging a lot of time here after this line, for example at the second item, we turn in there and onTick when you move the cursor to the debug mode in the position, it shows 0 element and writes elapsedTime. I removed the method onTick, but the first element is still keeping time.

if(bundleIsItStart){
                holder.stop.setEnabled(true);
                holder.start.setEnabled(false);
                holder.chronometer.setBase(base); --- after this
                holder.chronometer.start();
            }
Dennis Zinkovski
  • 1,821
  • 3
  • 25
  • 42

1 Answers1

0

Try putting this on the manifest file, remembering to change the name and label to the correspondent:

<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
Joaquin Iurchuk
  • 5,499
  • 2
  • 48
  • 64
  • You mean this android:configChanges="orientation|keyboardHidden|screenSize">, unfortunately its not work for me, activity should be recreated on rotation. Thank you for your answer! – Dennis Zinkovski Apr 12 '15 at 21:43
  • Then make use of the Bundle. Store and restore important values from there – Joaquin Iurchuk Apr 12 '15 at 21:45
  • i already use them, but i didnt show this code because my problem is not in saving data, data is saving correctly. Why first item of listView counting an elapsed time - this is my problem. I cant understand why when adapters method getView() located on position 1 or 2 after holder.chronometer.setBase(base); initialize for position 0 elapsed time. This problem is more visual character – Dennis Zinkovski Apr 12 '15 at 21:53