2016-05-05 54 views
0
public class ItemMemberAdapter extends BaseAdapter{ 

     ArrayList<Item> objects; 
     Context context; 
     Item itm; 
     MyHolder holder; 
     //timer 
     long starttime = 0L; 
     long timeInMilliseconds = 0L; 
     long timeSwapBuff = 0L; 
     long updatedtime = 0L; 
     int t = 1; 
     int secs = 0; 
     int mins = 0; 
     int milliseconds = 0; 
     Handler handler = new Handler(); 
     //timer 

     public ItemMemberAdapter(Context context, ArrayList<Item> objects) { 
      this.context=context; 
      this.objects=objects; 
     } 

     @Override 
     public int getCount() { 
      return objects.size(); 
     } 

     @Override 
     public Item getItem(int position) { 
      return objects.get(position); 
     } 

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 
     class MyHolder{ 
      TextView m_time,l_time,name; 
      Button s_btn,l_btn; 
     } 

     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      View v = convertView; 

      if(v==null) { 
       LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
       v=inflater.inflate(R.layout.item_member_list_app,null); 
       holder=new MyHolder();   
       holder.m_time= (TextView)v.findViewById(R.id.textView4); 
       holder.l_time= (TextView) v.findViewById(R.id.lap_time); 
       holder.name= (TextView) v.findViewById(R.id.textView5); 
       holder.s_btn=(Button)v.findViewById(R.id.strt_btn); 
       holder.l_btn=(Button)v.findViewById(R.id.lap_btn); 

       v.setTag(holder); 
      }else { 
       holder= (MyHolder) v.getTag(); 
      } 
      itm=null; 
      itm=getItem(position); 
      final String nm = itm.getMember_name(); 
      final String t = itm.getTimerOne(); 

      holder.name.setText(nm); 
      holder.m_time.setText(t); 
      final Runnable updateTimer = new Runnable() { 

       public void run() { 

        timeInMilliseconds = SystemClock.uptimeMillis() - starttime; 

        updatedtime = timeSwapBuff + timeInMilliseconds; 

        secs = (int) (updatedtime/1000); 
        mins = secs/60; 
        secs = secs % 60; 
        milliseconds = (int) (updatedtime % 1000); 
        String stime="" + mins + ":" + String.format("%02d", secs) + ":" 
          + String.format("%03d", milliseconds); 
        Log.d("timea", stime); 
        holder.l_time.setText(stime); 
        handler.postDelayed(this, 0); 
       } 
      }; 
      holder.s_btn.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Toast.makeText(context,"Name : "+nm+" Time : "+t,Toast.LENGTH_SHORT).show(); 
        starttime = SystemClock.uptimeMillis(); 
        handler.postDelayed(updateTimer, 0); 
       } 
      }); 

      return v; 
     } 

    } 

工作嗯,這是我的適配器,現在的問題是,當我點擊開始按鈕,然後計時器開始,我可以看到日誌,但該日誌值未在TextView的設置...計時器不適配器

回答

1

您的代碼架構很奇怪。

  • 將您的按鈕的onclick行動適配器外

  • 不要在活動運行Runnable內部適配器

  • 測量時間,並使用通過數據陣列適配器adapter.notifyDataSetChanged()

適配器只應以正確的方式收集和顯示數據。所有的數據操作應該在適配器之外。你沒有得到價值,因爲你沒有在你的案例中使用UI線程。然而,修復它在有史以來最糟糕的想法的UI線程上工作。用上面所寫的步驟對您的代碼進行重構。

+0

我曾嘗試過...但如何將數據傳遞給適配器在數組中.. ???????????? –

+0

您可以使用ArrayAdapter或使用setData()方法創建自定義適配器。您只需在適配器和刷新視圖中替換數據集即可。 – Fiil

+0

它將取代整個列表視圖數據,, ..但什麼只有單一的看法? –