Chronometer in RecyclerView : Timer became 00:00:00 when scrolling

778 Views Asked by At

I following this 1 ,2 an this 3 . I have an RecyclerView with CardView. I have add an timer using Chronometer. The aplication work like this:

enter image description here

There is first condition of CardView with Green Color. When User choose one, the color of change into Yellow.

I want make if the timer in 00:30:00(HH:MM:SS) the Color of CardView change color again into Blue. And when the timer in 01:00:00(HH:MM:SS), the color of CardView change again into Brown.

I following that tutor(above) But when i scroll down the RecyclerView , the timer (Choronometer) is became 00:00:00 again . I dont know where is the wrong.

Activity.java

private static final int THIRTYMINUTES = 1800000;
private static final int HOUR = 3600000;

int h,m,s;

...........
...........

@Override
        public void OnRecyclerViewItemBind(final Adapter.ViewHolder holder, final int position) {
            holder.txt_time_occ.setBase(SystemClock.elapsedRealtime());

            try {
                JSONObject currTable = filteredTableList.getJSONObject(position);

                holder.txt_no_table.setText(currTable.getString("tischnr"));
                holder.txt_no_table_occ.setText(currTable.getString("tischnr"));
                holder.txt_guest_name_occ.setText("");
                holder.txt_pax_occ.setText("");
                holder.txt_bill_occ.setText("");

                holder.txt_time_occ.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener(){
                    @Override
                    public void onChronometerTick(Chronometer cArg) {
                        long time = SystemClock.elapsedRealtime() - cArg.getBase();
                        h = (int)(time /3600000);
                        m = (int)(time - h*3600000)/60000;
                        s = (int)(time - h*3600000 - m*60000)/1000 ;
                        String hh = h < 10 ? "0"+h: h+"";
                        String mm = m < 10 ? "0"+m: m+"";
                        String ss = s < 10 ? "0"+s: s+"";
                        cArg.setText(hh+":"+mm+":"+ss);
                        holder.txt_time_occ.setText(String.format("%02d:%02d:%02d", h, m, s));

                        if (time > t1){
                            holder.itemView.setBackgroundResource(R.color.colorBrownTableOcc_3);
                            holder.title_layout.setBackgroundResource(R.color.colorBrownTableOcc_4);
                        }

                        if (time > t2) {
                            holder.itemView.setBackgroundResource(R.color.colorRedTableOcc);
                            holder.title_layout.setBackgroundResource(R.color.colorDeepRedTableOcc);
                        }
                    }
                });


                int queasy33Index = ProgramMethod.getJSONArrayIndex(queasy33List,"number2", currTable.getInt("tischnr"));
                if (queasy33Index >= 0) {
                    holder.txt_guest_name_occ.setText(queasy33List.getJSONObject(queasy33Index).getString("char2"));
                    holder.txt_pax_occ.setText(queasy33List.getJSONObject(queasy33Index).getString("number3"));
                }

                int billIndex = ProgramMethod.getJSONArrayIndex(billList, "tischnr", currTable.getInt("tischnr"));
                if(billIndex >= 0) {
                holder.txt_bill_occ.setText(billList.getJSONObject(billIndex).getString("saldo"));
                }

                if (currTable.has("selected") && currTable.getBoolean("selected")) {
                    holder.itemView.setBackgroundResource(R.color.colorYellowTableOcc_6);
                    holder.title_layout.setBackgroundResource(R.color.colorYellowTableOcc_7);
                    holder.txt_time_occ.setBase(SystemClock.elapsedRealtime());
                    holder.txt_time_occ.start();
                    holder.txt_no_table.setVisibility(View.INVISIBLE);
                    holder.txt_no_table_occ.setVisibility(View.VISIBLE);
                    holder.txt_bill_occ.setVisibility(View.VISIBLE);
                    holder.txt_pax_occ.setVisibility(View.VISIBLE);
                    holder.txt_time_occ.setVisibility(View.VISIBLE);
                    holder.txt_guest_name_occ.setVisibility(View.VISIBLE);
                    holder.cardview_table.setCardElevation(24);

                } else {
                    holder.txt_time_occ.stop();
                    holder.txt_time_occ.setBase(SystemClock.elapsedRealtime());
                    holder.itemView.setBackgroundResource(R.color.colorTableGreen);
                    holder.title_layout.setBackgroundResource(R.color.colorTableGreen);
                    holder.txt_no_table.setVisibility(View.VISIBLE);
                    holder.txt_no_table_occ.setVisibility(View.INVISIBLE);
                    holder.txt_bill_occ.setVisibility(View.INVISIBLE);
                    holder.txt_pax_occ.setVisibility(View.INVISIBLE);
                    holder.txt_time_occ.setVisibility(View.INVISIBLE);
                    holder.txt_guest_name_occ.setVisibility(View.INVISIBLE);
                    holder.cardview_table.setCardElevation(1);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public int OnRecyclerViewItemCount() {
            return filteredTableList.length();
        }
    });
}

Adapter.java

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {

public static final int view1 = 0;
public static final int view2 = 1;

private static OnRecyclerViewItemClickedListener recyclerViewItemClickedListener;

public void setOnRecyclerViewClickedListener (OnRecyclerViewItemClickedListener l) {
    recyclerViewItemClickedListener = l;
}

public interface OnRecyclerViewItemClickedListener {
    void OnRecyclerViewItemClicked(int position);
    void OnRecyclerViewItemBind(ViewHolder holder, int position);
    int OnRecyclerViewItemCount();
}

public Adapter() {
    super();
}


@Override
public Adapter.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {



    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.table_item_empty, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    recyclerViewItemClickedListener.OnRecyclerViewItemBind(holder, position);
}

@Override
public int getItemCount() {
    return recyclerViewItemClickedListener.OnRecyclerViewItemCount();
}


public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public TextView txt_no_table;
    public TextView txt_no_table_occ;
    public TextView txt_pax_occ;
    public TextView txt_guest_name_occ;
    public TextView txt_bill_occ;
    public Chronometer txt_time_occ;
    public CardView cardview_table;



    public ViewHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);

        txt_no_table = (TextView) itemView.findViewById(R.id.txt_no_table_empty);
        txt_no_table_occ = (TextView) itemView.findViewById(R.id.txt_no_table);
        txt_pax_occ = (TextView) itemView.findViewById(R.id.txt_pax);
        txt_guest_name_occ = (TextView)itemView.findViewById(R.id.txt_guestname);
        txt_bill_occ = (TextView) itemView.findViewById(R.id.txt_bill);
        txt_time_occ = (Chronometer) itemView.findViewById(R.id.txt_time);
        cardview_table = (CardView) itemView.findViewById(R.id.table_card_empty);
    }

    @Override
    public void onClick(View itemView) {
        recyclerViewItemClickedListener.OnRecyclerViewItemClicked(getAdapterPosition());
   }
}

Any answer will helpful for me. Thanks before

0

There are 0 best solutions below