如何在适配器

时间:2018-02-21 14:14:21

标签: java android android-arrayadapter countdown countdowntimer

您好我从服务器获得一些时间,我在模型类中保存。 我需要按倒计时格式购买时间。例如,如果我有时间(2018-2-23 09:30 pm),那么我想要离开的时间(有些日子,某些时间,某些时间,某些时间)。 我使用过CountDownTimer类,除了UI更改之外,我已经完成了所有事情。 这是我的代码: 这里我从getView方法得到的位置。 很长一段时间是我的转换服务器日期和时间。

这里我要设置数据: viewHolder.mytextView  通常我会通过viewHolder.mytextView.setText(myModel.get(position).serverTime

我得到倒数时间计数。

public class Adapter extends ArrayAdapter {

    private ArrayList<MyModel> myModel;
    private Holder viewHolder;

    int resource;
    private Activity context;
    CountDownTimer count_Time_left;
    long currentTime;
    private String timerChanger ="";
    public Adapter(@NonNull Context context, int resource,
                         @NonNull private ArrayList<MyModel> myModel;) {
        super(context, resource, myModel);
        this.context=(Activity)context;
        this.resource=resource;
        this.myModel=myModel;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        viewHolder =null;
        View rootView = convertView;
           //pref = PreferenceManager.getDefaultSharedPreferences(mContext);
        if (rootView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            rootView = inflater.inflate(R.layout.view, parent, false);
            viewHolder = new League_Holder();    
            viewHolder.time_text =(TextView)rootView.findViewById(R.id.time_text);
            setcountDown(position);
            rootView.setTag(viewHolder);
        } else {
            viewHolder = (Holder)rootView.getTag();
        }
        return rootView;
    }



    private void setcountDown(int position){
        SimpleDateFormat sdf_time = new SimpleDateFormat("yyyy-MM-dd hh:mmaa");

        try {
            Date date = sdf_time.parse(leagueLists.get(position).getLeague_start_date() +" "+
                    leagueLists.get(position).getLeague_start_time());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            /*Current time miles*/
            startcountdowntimer(sdf.parse(sdf.format(date)).getTime(),position);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    private void startcountdowntimer(long time, final int position) {
        // TODO Auto-generated method stub
        long time_total = time;
        long time_divide = 1000;

        Log.e("Millisecond is", String.valueOf(time_total));
         count_Time_left = new CountDownTimer(time_total, time_divide) {

            @Override
            public void onTick(long millisUntilFinished) {
                // TODO Auto-generated method stub  
                showcountDown(millisUntilFinished,position);
            }
            @Override
            public void onFinish() {
            // TODO Auto-generated method stub
            }
        }.start();
    }

    private  void showcountDown(long millisUntilFinished ,int position) {
        Log.e("Position", String.valueOf(position));
             /*Current time miles*/
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            currentTime = sdf.parse(sdf.format(new Date())).getTime();
                long diff = millisUntilFinished - currentTime;
                long diffSeconds = (diff / 1000 % 60);
                long diffMinutes = (diff / (60 * 1000) % 60);
                long diffHours = (diff / (60 * 60 * 1000) % 24);
                long diffDays = (diff / (24 * 60 * 60 * 1000));
                timerChanger = diffDays+" day " +diffHours+" Hour "+diffMinutes+" minute "+diffSeconds+" sec";
                viewHolder.time_text.setText(timerChanger);     
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    static class Holder {
        TextView time_text
;
    }

}

这是我的模特课:

 public class MyModel {
       private String start_date;
        private String start_time;

        public MyModel(
                         String start_date, String start_time,
                         ) {
            this.league_start_date = league_start_date;
            this.start_time= start_time;
        }

        public String start_date() {
            return start_date;
        }

        public void start_date(String start_date) {
            this.start_date= start_date;
        }

        public String end_time() {
            return start_time;
        }

        public void start_time(String start_time) {
            this.start_time= start_time;
        }
        }
    }

0 个答案:

没有答案