2013-07-31 105 views
1

我已創建了倒計時器列表視圖,結果表明在第一次好,但是當滾動它被洗牌的名單,結果得到錯誤的。CountDownTimer問題列表視圖的Android

適配器類:

public class EventsListAdapter extends BaseAdapter { 

    LayoutInflater inflater; 
    Activity context; 

    List<MyEvents> list; 

    long startTime = 0L; 
    long countUp; 
    String hrs = "00"; 
    String mins = "00"; 
    String secs = "00"; 
    private MyCustomTimer myTimer; 
    public EventsListAdapter(Activity context, List<MyEvents> list) { 
     this.context = context; 
     inflater = context.getLayoutInflater(); 
     this.list = list; 
    } 

    public void setActivityList(List<MyEvents> LoadList) { 
     list = LoadList; 
    } 

    public int getCount() { 
     return list.size(); 
    } 

    public Object getItem(int position) { 
     return list.get(position); 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    static class ViewHolder { 
     TextView name; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     MyEvents eventsData = list.get(position); 

     View rowView = convertView; 
     ViewHolder viewHolder; 
     if (rowView == null) { 
      LayoutInflater inflater = context.getLayoutInflater(); 
      rowView = inflater.inflate(R.layout.likes_inflate, null); 
      viewHolder = new ViewHolder(); 
      viewHolder.name = (TextView) rowView.findViewById(R.id.lblName); 
      if (eventsData.isStarted()){ 
       myTimer = new MyCustomTimer(viewHolder.name); 
       myTimer.setTimer(eventsData.start_date); 
      } 
      rowView.setTag(viewHolder); 
     }else{ 
      viewHolder = (ViewHolder) rowView.getTag(); 
     } 

     return rowView; 
    } 
} 

CountDownTimer類:

class MyCustomTimer{ 
     TextView tv; 
     public MyCustomTimer(TextView tv) { 
      this.tv = tv; 
     } 

     void setTimer(final long time) { 
      new CountDownTimer(time, 1000) { 
       public void onTick(long millisUntilFinished) { 
        //Set formatted date to your TextView     
        tv.setText(Utilities.getDurationBreakdown(millisUntilFinished)); 

       } 
       public void onFinish() { 
        tv.setText("Done!"); 
       } 
      }.start(); 
     } 
    } 

是anythig錯誤與此代碼?請建議。

回答

0

當你永遠滾動列表視圖,getView()被調用。所以櫃檯再次啓動。保持計數器的哈希映射變量。在getView()中檢查這個變量,然後根據它設置計數器的值。

+0

我已經檢查了計數器已經啓動與否,如果它開始我不叫計時器。我編輯了代碼。仍然是同樣的問題。 – fargath

+2

如果您發現計數器已啓動,請將計數器再次設置爲其離開的時間。 也就是說,取一個變量併爲其分配計數器時間。如果計數器是10,那麼變量應該是10.所以在getView()分配計數器與該變量。 希望你得到這個。 –

+0

我得到了解決方案,我分配了一個變量來設置計數器,併爲定時器上的每個滴答作出適配器notifydatasetchanged()。現在它效果很好。 – fargath