旋转屏幕后,总是先计时器开始计数 [英] After rotating screen, always first timer beginning a count

查看:128
本文介绍了旋转屏幕后,总是先计时器开始计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

半个月我试图解决这个问题,它的我的第二个解决方案,我变老错误。
我的目标是编写一个带定时器的ListView与启动和停止按钮,每一行中,旋转屏幕后,所有的计时器应能正常工作,但在pre-解决方案旋转屏幕之后,第一个位置的列表视图如何让时间过去/更低的位置。
至于这两个解决方案的链接,我看到getView()方法只是逻辑,我肯定是主要的问题就是100%。

Half month i trying fix this problem, its my second solution and i get old error. My goal is to write a listView with timer in every row with Start and Stop buttons, after rotating screen all timers should work correctly, but how in pre-solution after rotation screen, first position in listview get time last/lower position. As for link with these two solution i see just logic of getView() method, and i'm on 100% sure that is the main problem.

任何人可以帮助我这个,我是处于僵局。 code的问题我喜欢:

Can anybody help me with this, i am at an impasse. Problematic piace of code:

 if(isItStart.get(position)){
            holder.stop.setEnabled(true);
            holder.start.setEnabled(false);
            handler.postDelayed(updateTimeThread,0);
        }

下面是完整的类。

 ListView listView;
MyAdapter adapter;
Handler handler;
SQLiteDatabase db;
List<Tracker> trackerList;
Tracker tracker;
List<Boolean> isItStart,historyIsItStart;
List<Long> startTime,historyStartTime;
List<Long> lastPauseList,historyLastPauseList;
List<Long> updateTimeList, historyUpdateTimeList;
List<Long> daysList,historyDayList;
List<Long> hoursList,historyHoursList;
List<Long> minutesList,historyMinutes;
List<Long> secondsList,historySecondsList;
int trackerCount;


static final String LOG_TAG = "myTag";


@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    handler = new Handler();
    db = RemindMe.db;
    trackerList = Tracker.getListAll(db);
    trackerCount=trackerList.size();
    initLists();
    for (int i = 0; i < trackerCount; i++) {
        startTime.add(0L);
        lastPauseList.add(0L);
        updateTimeList.add(0L);
        daysList.add(0L);
        hoursList.add(0L);
        minutesList.add(0L);
        secondsList.add(0L);
        isItStart.add(false);

            historyStartTime.add(startTime.get(i));
            historyLastPauseList.add(lastPauseList.get(i));
            historyUpdateTimeList.add(updateTimeList.get(i));
            historyDayList.add(daysList.get(i));
            historyHoursList.add(hoursList.get(i));
            historyMinutes.add(minutesList.get(i));
            historySecondsList.add(secondsList.get(i));
            historyIsItStart.add(isItStart.get(i));
    }

    listView = (ListView)findViewById(R.id.listView);
    String[] from = {Tracker.COL_NAME,Tracker.COL_ELAPSED_TIME,Tracker.COL_ELAPSED_TIME,Tracker.COL_ELAPSED_TIME,Tracker.COL_ELAPSED_TIME};
    int[] to = {R.id.tvName,R.id.tvDays,R.id.tvHours,R.id.tvMinutes,R.id.tvSeconds};
    adapter = new MyAdapter(this,R.layout.list_item,Tracker.getAll(db),from,to,0);
    adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            long day,hour,min,sec;
            long time = cursor.getLong(columnIndex);
            switch(view.getId()){
                case R.id.tvDays:
                    TextView days = (TextView)view;
                    days.setText("days");
                    return true;
                case R.id.tvHours:
                    TextView hours = (TextView)view;
                    hours.setText("hours");
                    return true;
                case R.id.tvMinutes:
                    TextView minutes = (TextView)view;

                    minutes.setText("min");
                    return true;
                case R.id.tvSeconds:
                    TextView seconds = (TextView)view;
                    if(time!=0){
                        sec = time/1000;
                        seconds.setText(String.valueOf(sec));
                    }else{
                        seconds.setText("null");
                    }
                    return true;
            }
            return false;
        }
    });
    listView.setAdapter(adapter);
    getSupportLoaderManager().initLoader(1,null,this).forceLoad();

}
void initLists(){
    startTime = new ArrayList<Long>(trackerCount);
    lastPauseList = new ArrayList<Long>(trackerCount);
    updateTimeList = new ArrayList<Long>(trackerCount);
    daysList = new ArrayList<Long>(trackerCount);
    hoursList = new ArrayList<Long>(trackerCount);
    minutesList = new ArrayList<Long>(trackerCount);
    secondsList = new ArrayList<Long>(trackerCount);
    isItStart = new ArrayList<Boolean>(trackerCount);


    historySecondsList = new ArrayList<Long>(trackerCount);
    historyMinutes = new ArrayList<Long>(trackerCount);
    historyHoursList = new ArrayList<Long>(trackerCount);
    historyDayList = new ArrayList<Long>(trackerCount);
    historyUpdateTimeList = new ArrayList<Long>(trackerCount);
    historyLastPauseList = new ArrayList<Long>(trackerCount);
    historyStartTime = new ArrayList<Long>(trackerCount);
    historyIsItStart = new ArrayList<Boolean>(trackerCount);

}

@Override
public void onClick(View v) {
    Intent intent = new Intent(this,AddTrack.class);
    startActivity(intent);
}

@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
    return new TrackerLoader(this,db);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    adapter.swapCursor(cursor);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {

}
static class TrackerLoader extends android.support.v4.content.CursorLoader{
    SQLiteDatabase db;
    TrackerLoader(Context context,SQLiteDatabase db){
        super(context);
        this.db=db;
    }

    @Override
    public Cursor loadInBackground() {
       return Tracker.getAll(db);
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Log.d(LOG_TAG, "onSavedInstanceState---------------------------------------------------------------!");

    for (int i = 0; i <trackerCount ; i++) {
        historyStartTime.set(i,startTime.get(i));
        historyLastPauseList.set(i, lastPauseList.get(i));
        historyUpdateTimeList.set(i,updateTimeList.get(i));
        historyDayList.set(i, daysList.get(i));
        historyHoursList.set(i,hoursList.get(i));
        historyMinutes.set(i, minutesList.get(i));
        historySecondsList.set(i,secondsList.get(i));
        historyIsItStart.set(i, isItStart.get(i));

        outState.putSerializable("startTime " + i, historyStartTime.get(i));
        outState.putSerializable("lastPause " + i, historyLastPauseList.get(i));
        outState.putSerializable("updateTime " + i, historyUpdateTimeList.get(i));
        outState.putSerializable("dayList " + i, historyDayList.get(i));
        outState.putSerializable("hoursList " + i, historyHoursList.get(i));
        outState.putSerializable("minutesList " + i, historyMinutes.get(i));
        outState.putSerializable("secondsList " + i, historySecondsList.get(i));
        outState.putSerializable("isItStart " + i, historyIsItStart.get(i));

        Log.d(LOG_TAG, "startTime " + getTime((Long) outState.getSerializable("startTime " + i)));
        Log.d(LOG_TAG, "lastPause " + getTime((Long) outState.getSerializable("lastPause " + i)));
        Log.d(LOG_TAG, "updateTime " + getTime((Long) outState.getSerializable("updateTime " + i)));
        Log.d(LOG_TAG, "dayList " + getTime((Long) outState.getSerializable("dayList " + i)));
        Log.d(LOG_TAG, "hoursList " + getTime((Long) outState.getSerializable("hoursList " + i)));
        Log.d(LOG_TAG, "minutesList " + getTime((Long) outState.getSerializable("minutesList " + i)));
        Log.d(LOG_TAG, "secondsList " +  outState.getSerializable("secondsList " + i));
        Log.d(LOG_TAG, "isItStart " + outState.getSerializable("isItStart " + i));
        Log.d(LOG_TAG, "position " + i);
        Log.d(LOG_TAG,"-----------------------------------!");
    }
    Log.d(LOG_TAG,"END onSavedInstanceState-------------------------------------------------------------!");
    for (int i = 0; i < trackerCount; i++) {
        Log.d(LOG_TAG,"secondsList "+i+ " "+secondsList.get(i));
    }
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    Log.d(LOG_TAG, "onRestoreInstanceState-------------------------------------------------------!");

    for (int i = 0; i <trackerCount ; i++) {
        historyStartTime.set(i,(Long)savedInstanceState.getSerializable("startTime "+i));
        historyLastPauseList.set(i,(Long)savedInstanceState.getSerializable("lastPause "+i));
        historyUpdateTimeList.set(i,(Long)savedInstanceState.getSerializable("updateTime "+i));
        historyDayList.set(i,(Long)savedInstanceState.getSerializable("dayList "+i));
        historyHoursList.set(i,(Long)savedInstanceState.getSerializable("hoursList "+i));
        historyMinutes.set(i,(Long)savedInstanceState.getSerializable("minutesList "+i));
        historySecondsList.set(i,(Long)savedInstanceState.getSerializable("secondsList "+i));
        historyIsItStart.set(i,(Boolean)savedInstanceState.getSerializable("isItStart "+i));

        startTime.set(i,historyStartTime.get(i));
        lastPauseList.set(i,historyLastPauseList.get(i));
        updateTimeList.set(i,historyUpdateTimeList.get(i));
        daysList.set(i,historyDayList.get(i));
        hoursList.set(i,historyHoursList.get(i));
        minutesList.set(i,historyMinutes.get(i));
        secondsList.set(i,historySecondsList.get(i));
        isItStart.set(i, historyIsItStart.get(i));


        Log.d(LOG_TAG, "startTime " + getTime((Long) savedInstanceState.getSerializable("startTime " + i)));
        Log.d(LOG_TAG,"lastPause " + getTime((Long) savedInstanceState.getSerializable("lastPause " + i)));
        Log.d(LOG_TAG,"updateTime " + getTime((Long) savedInstanceState.getSerializable("updateTime " + i)));
        Log.d(LOG_TAG,"dayList " + getTime((Long) savedInstanceState.getSerializable("dayList " + i)));
        Log.d(LOG_TAG,"hoursList " + getTime((Long) savedInstanceState.getSerializable("hoursList " + i)));
        Log.d(LOG_TAG,"minutesList " + getTime((Long) savedInstanceState.getSerializable("minutesList " + i)));
        Log.d(LOG_TAG, "secondsList " + savedInstanceState.getSerializable("secondsList " + i));
        Log.d(LOG_TAG,"isItStart "+savedInstanceState.getSerializable("isItStart " + i));
        Log.d(LOG_TAG,"position "+i);
        Log.d(LOG_TAG,"----------------------------------------------------------------");
    }
    Log.d(LOG_TAG,"END onRestoreIntstanceState-------------------------------------------------------------!");
}

private class MyAdapter extends SimpleCursorAdapter{
    Context context;
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    MyAdapter(Context context,int resourceID,Cursor cursor,String[] from,int[]to,int flags){
        super(context, resourceID, cursor, from, to, flags);
        this.context = context;
    }
    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        View row = convertView;
        final ViewHolder holder;
        tracker = trackerList.get(position);
        if(row==null){
            holder = new ViewHolder();
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(R.layout.list_item,parent,false);
            holder.name= (TextView)row.findViewById(R.id.tvName);
            holder.days = (TextView)row.findViewById(R.id.tvDays);
            holder.hours = (TextView)row.findViewById(R.id.tvHours);
            holder.minutes = (TextView)row.findViewById(R.id.tvMinutes);
            holder.seconds = (TextView)row.findViewById(R.id.tvSeconds);
            holder.start = (Button)row.findViewById(R.id.btStart);
            holder.stop = (Button)row.findViewById(R.id.btStop);
            row.setTag(holder);
        }else{
            holder = (ViewHolder)row.getTag();
        }
        holder.start.setEnabled(true);
        holder.stop.setEnabled(false);
        holder.name.setText(tracker.getName());
                     final Runnable updateTimeThread = new Runnable() {
            @Override
            public void run() {
                    updateTimeList.set(position, (System.currentTimeMillis() - startTime.get(position)) + lastPauseList.get(position));
                    secondsList.set(position, updateTimeList.get(position) / 1000);
                    minutesList.set(position, secondsList.get(position) / 60);
                    hoursList.set(position, minutesList.get(position) / 60);

                    secondsList.set(position, (secondsList.get(position) % 60));
                    minutesList.set(position, (minutesList.get(position) % 60));
                    hoursList.set(position, (hoursList.get(position) % 24));

                    holder.days.setText(String.format("%04d", daysList.get(position)));
                    holder.hours.setText(String.format("%02d", hoursList.get(position)));
                    holder.minutes.setText(String.format("%02d", minutesList.get(position)));
                    holder.seconds.setText(String.format("%02d", secondsList.get(position)));
                    handler.postDelayed(this, 0);
            }
        };
        if(isItStart.get(position)){
            holder.stop.setEnabled(true);
            holder.start.setEnabled(false);
            handler.postDelayed(updateTimeThread,0);
        }
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.btStart:
                        startTime.set(position,System.currentTimeMillis());
                        handler.post(updateTimeThread);
                        holder.start.setEnabled(false);
                        holder.stop.setEnabled(true);
                        isItStart.set(position,true);
                        break;
                    case R.id.btStop:
                        lastPauseList.set(position, updateTimeList.get(position));
                        handler.removeCallbacks(updateTimeThread);
                        holder.stop.setEnabled(false);
                        holder.start.setEnabled(true);
                        isItStart.set(position,false);
                        break;
                }
            }
        };
        holder.start.setOnClickListener(onClickListener);
        holder.stop.setOnClickListener(onClickListener);
        return row;
    }

    class ViewHolder{
        TextView name,days,hours,minutes,seconds;
        Button start,stop;
    }

}
String getTime(long time){
    int hours = (int)(time/3600000);
    int minutes = (int)(time -hours*3600000)/60000;
    int seconds = (int)(time-hours*3600000-minutes*60000)/1000;
    String hour = (hours<9?"0"+hours:hours).toString();
    String min = (minutes<9?"0"+minutes:minutes).toString();
    String sec = (seconds<9?"0"+seconds:seconds).toString();
    return ""+hour+":"+min+":"+sec;
}

}

推荐答案

添加的android:configChanges =方向|屏幕尺寸在的manifest.xml和删除您OnRestore中和onSaved

Add android:configChanges="orientation|screenSize" in manifest.xml and delete your onRestore and onSaved.

这篇关于旋转屏幕后,总是先计时器开始计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆