定时器在ADAPTER中运行不正常

时间:2016-05-05 05:22:58

标签: java android timer

 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;
        }

    }

这是我的适配器,现在的问题是,当我点击开始按钮然后启动计时器,我可以看到Log但是没有在textView上设置LOG值

1 个答案:

答案 0 :(得分:1)

您的代码架构很奇怪。

  • 在适配器外移动按钮onclick动作

  • 请勿在适配器

  • 内运行Runnable
  • 测量活动时间,并使用adapter.notifyDataSetChanged()

  • 将数据传递到数组中的适配器

适配器应该只以正确的方式收集和显示数据。所有数据操作都应该在适配器之外。您没有获得值,因为在您的情况下您不在UI线程上运行。然而,在最坏的想法中修复它以在UI线程上工作。使用上面的步骤对代码进行重构。