How to maintain the state of button cutom listview in android

Posted by Akshay on Stack Overflow See other posts from Stack Overflow or by Akshay
Published on 2012-12-14T05:02:20Z Indexed on 2012/12/14 5:03 UTC
Read the original article Hit count: 320

I have custom ListView with three TextView three Button and three Chronometer. And the situation is I am loading the ListView properly.But while loading ListView I am disabling some button in the ListView by checking one parameter. Up to this point ListView is showing it's row properly.

But when I am scrolling the ListView at that time previously enabled Button are getting disabled.What I am doing wrong I am not getting can one please point out my mistake Or any suggestion.

Here is my Adapter class.

        public class OrderSmartKitchenAdapter extends BaseAdapter {
    private int flagDeliveryComplete = 0;
    private int flagPreparationComplete = 0;
    private int flagPreparationStarted = 0;
    private List<OrderitemdetailsBO> list = new ArrayList<OrderitemdetailsBO();
    private int orderStatus;

    public OrderSmartKitchenAdapter() {
        // TODO Auto-generated constructor stub         
    }

    public void setOrderList(List<OrderitemdetailsBO> orderList) {
        this.list = orderList;
    }       

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        Log.i("OrderItemList Size :-", Integer.toString(list.size()));
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View convertView,ViewGroup parent) {
        // TODO Auto-generated method stub

        final ViewHolder viewHolder ; 


        if (convertView == null) 
        {
            layoutInflater = LayoutInflater.from(myContext);
            convertView = layoutInflater.inflate(R.layout.table_row_view,null);
            viewHolder = new ViewHolder();

            viewHolder.txtTableNumber = (TextView) convertView.findViewById(R.id.txtTableNumber);
            viewHolder.txtMenuItem = (TextView) convertView.findViewById(R.id.txtMenuItem);
            viewHolder.txtQuantity = (TextView) convertView.findViewById(R.id.txtQuantity);
            viewHolder.txtOrderAcceptanceTime = (TextView) convertView.findViewById(R.id.txtOrderAcceptanceTime);
            viewHolder.txtElapsedTimeOfOrderAcceptance = (Chronometer) convertView.findViewById(R.id.txtElapsedTimeOfOrderAcceptance);
            viewHolder.btnPreparationStart = (Button) convertView.findViewById(R.id.btnPreparationStart);
            viewHolder.btnPreparationStart.setTag(position);
            viewHolder.txtElapsedTimeForPreparation = (Chronometer) convertView.findViewById(R.id.txtElapsedTimeForPrepatration);
            viewHolder.btnPreparationComplete = (Button) convertView.findViewById(R.id.btnPreparationCompleted);
            viewHolder.btnPreparationComplete.setTag(position);
            viewHolder.txtElapsedTimeForDeliveryComplete = (Chronometer) convertView.findViewById(R.id.txtElapsedTimeForCompleation);
            viewHolder.btnDeliveryComplete = (Button) convertView.findViewById(R.id.btnOrderComplete);
            viewHolder.btnDeliveryComplete.setTag(position);

            convertView.setTag(viewHolder);
        }

        else{

                viewHolder = (ViewHolder)convertView.getTag();
                viewHolder.btnDeliveryComplete.setTag(position);
                viewHolder.btnPreparationComplete.setTag(position);
                viewHolder.btnPreparationStart.setTag(position);
        }

        if (list.get(position) != null) {

            OrderitemdetailsBO orderitemdetailsBO = new OrderitemdetailsBO();
            orderitemdetailsBO = list.get(position);

            viewHolder.txtTableNumber.setText(orderitemdetailsBO.getOrderitemid().toString());
            viewHolder.txtMenuItem.setText(orderitemdetailsBO.getMenuitemname().toString());
            viewHolder.txtQuantity.setText(orderitemdetailsBO.getQuantity().toString());

            Log.i("Table Number :-", Long.toString(orderitemdetailsBO.getOrderitemid()));
            Log.i("Menu Name :-", orderitemdetailsBO.getMenuitemname().toString());
            Log.i("Quantity", orderitemdetailsBO.getQuantity().toString());

            Date acceptTime = new Date();
            acceptTime = orderitemdetailsBO.getOrderdatetime();

            viewHolder.txtOrderAcceptanceTime.setText(DateUtil.getDateAsString(acceptTime,"HH:mm"));
            Log.i("Order Accept Time :-", acceptTime.getMinutes() + ":"+ acceptTime.getSeconds());


            orderStatus = orderitemdetailsBO.getOrderstatus();

            Date preparationStartTime = new Date();
            preparationStartTime = orderitemdetailsBO.getPreparationstarttime();

            if(preparationStartTime != null)

            {
                Log.i("OrderSmartKitchenActivity", "2 Order Acceptance Time :-" + 
                        "Menu Item id "+ orderitemdetailsBO.getOrderitemid() 
                        + " Preparation Start time " + orderitemdetailsBO.getPreparationstarttime()                         
                    );

                viewHolder.txtElapsedTimeOfOrderAcceptance.stop();
                Log.i("Preparation Start Time :-",preparationStartTime.getMinutes() + ":"   + preparationStartTime.getSeconds());
                viewHolder.txtElapsedTimeOfOrderAcceptance.setText(DateUtil.getDateAsString(preparationStartTime,"MM:ss"));

                viewHolder.txtElapsedTimeOfOrderAcceptance.stop();


                viewHolder.btnPreparationStart.setEnabled(false);
                viewHolder.btnPreparationStart.setClickable(false);
                viewHolder.btnPreparationStart.setBackgroundColor(Color.LTGRAY);

            }

            else 
            {
                Long n = acceptTime.getTime();
                Log.i("OrderSmartKitchenActivity", "Order Acceptance Time :-" + 
                        "Menu Item id "+ orderitemdetailsBO.getOrderitemid() 
                        + " Acceptance time" +  Long.toString(n)
                        + " Preparation Start time " + orderitemdetailsBO.getPreparationstarttime()

                    );

                // Calculate Time difference
                viewHolder.txtElapsedTimeOfOrderAcceptance.setBase(SystemClock.elapsedRealtime() - System.currentTimeMillis() + n);
                viewHolder.txtElapsedTimeOfOrderAcceptance.getBase();
                viewHolder.txtElapsedTimeOfOrderAcceptance.start();     
                viewHolder.txtElapsedTimeOfOrderAcceptance.setFormat("%s");                                     
            }

            viewHolder.btnPreparationStart.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(final View v) {
                            // TODO Auto-generated method stub

                            if (flagPreparationStarted == 0) {
                                flagPreparationStarted++;

                                v.startAnimation(playAnimation());
                                handler.postDelayed(new Runnable() {

                                    @Override
                                    public void run() {
                                        // TODO Auto-generated method stub
                                        v.clearAnimation();
                                        Date currentTime = new Date();              

                                        // Set Preparation Start Time.
                                        viewHolder.txtElapsedTimeOfOrderAcceptance.stop();
                                        Date setTime = new Date(currentTime.getTime() * 1000);

                                        OrderitemdetailsBO orderitemdetailsBO = list.get(position); 
                                        orderitemdetailsBO.setPreparationstarttime(setTime);

                                        String orderDetails = "2";
                                        String getPosition = Integer.toString(position);
                                        viewHolder.btnPreparationStart.setBackgroundColor(Color.LTGRAY);
                                        new sendOrderStatusToServer().execute(orderDetails,getPosition);

                                    }
                                }, 5000);

                            }

                            else {

                                handler.removeCallbacksAndMessages(null);
                                v.clearAnimation();
                                flagPreparationStarted = 0;
                                Log.i("Handler Removed. :-", "Here");

                            } 
                        }
                    });

            String preparationTime = orderitemdetailsBO.getOrderpreparationtime();


            if(preparationTime != null && orderStatus  == order_preparationComplete)
            {
                viewHolder.txtElapsedTimeForPreparation.setText(preparationTime);
                viewHolder.txtElapsedTimeForPreparation.stop();

                viewHolder.btnPreparationComplete.getTag(position);
                viewHolder.btnPreparationComplete.setEnabled(false);
                viewHolder.btnPreparationComplete.setClickable(false);
                viewHolder.btnPreparationComplete.setBackgroundColor(Color.LTGRAY);
            }

            else if( orderStatus == order_preparationStart || orderStatus == orderReceived || orderStatus == order_delivered){

                Long n = acceptTime.getTime();
                Log.i("Preparation Start Time :-", Long.toString(n));

                viewHolder.txtElapsedTimeForPreparation.setBase(SystemClock.elapsedRealtime() - System.currentTimeMillis() + n);
                viewHolder.txtElapsedTimeForPreparation.getBase();
                viewHolder.txtElapsedTimeForPreparation.start();
                viewHolder.txtElapsedTimeForPreparation.setFormat("%s");            
            }

            viewHolder.btnPreparationComplete.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(final View v) {
                            // TODO Auto-generated method

                            if (flagPreparationComplete == 0) {
                                flagPreparationComplete++;

                                v.startAnimation(playAnimation());
                                handler.postDelayed(new Runnable() {

                                    @Override
                                    public void run() {
                                        // TODO Auto-generated method stub

                                        v.clearAnimation();

                                        OrderitemdetailsBO orderitemdetailsBO = list.get(position); 
                                        Date date = orderitemdetailsBO.getPreparationstarttime();

                                        if(date != null)
    {
                                            viewHolder.txtElapsedTimeForPreparation.stop();
                                            Date currentTime = new Date();
                                            Calendar calendar = Calendar.getInstance();

                                            int minute = calendar.get(Calendar.MINUTE);
                                            int second = calendar.get(Calendar.SECOND);

                                            orderitemdetailsBO.setOrderpreparationtime(calendar.get(Calendar.MINUTE) +":" +calendar.get(Calendar.SECOND));
                                            String orderDetails = "3";
                                            String getPosition = Integer.toString(position);
                                            viewHolder.btnPreparationComplete.setBackgroundColor(Color.LTGRAY);
                                            new sendOrderStatusToServer().execute(orderDetails,getPosition);
                                        }

                                        else
                                        {
                                            Toast.makeText(myContext, "Please Enter Preparation Start Time.", Toast.LENGTH_LONG).show();
                                        }                       

                                    }
                                }, 5000);

                            } else {

                                handler.removeCallbacksAndMessages(null);
                                v.clearAnimation();
                                flagPreparationComplete = 0;

                            }
                        }
                    });

            String deleveredTime = orderitemdetailsBO.getOrderdeliverytime();

            if(deleveredTime != null && orderStatus == order_delivered)
            {

                Date delevered = new Date(Long.parseLong(deleveredTime));                   

                viewHolder.txtElapsedTimeForPreparation.setText(DateUtil.getDateAsString(delevered,"MM:ss"));
                Log.i("Preparation Start Time :-", delevered.getMinutes()+":"+delevered.getSeconds());
                viewHolder.txtElapsedTimeForPreparation.stop();

                viewHolder.btnDeliveryComplete.getTag(position);
                viewHolder.btnDeliveryComplete.setEnabled(false);
                viewHolder.btnDeliveryComplete.setClickable(false);
                viewHolder.btnDeliveryComplete.setBackgroundColor(Color.LTGRAY);
            }

            else if(orderStatus == 3 || orderStatus == 2 || orderStatus == 1)               
            {                                   
                Long n = acceptTime.getTime();
                Log.i("Preparation Start Time :-", Long.toString(n));

                viewHolder.txtElapsedTimeForDeliveryComplete.setTag(list.get(position));
                viewHolder.txtElapsedTimeForDeliveryComplete.setBase(SystemClock.elapsedRealtime() - System.currentTimeMillis() + n);
                viewHolder.txtElapsedTimeForDeliveryComplete.getBase();
                viewHolder.txtElapsedTimeForDeliveryComplete.start();
                viewHolder.txtElapsedTimeForDeliveryComplete.setFormat("%s");

            }

            viewHolder.btnDeliveryComplete.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(final View v) {
                            // TODO Auto-generated method stub

                            if (flagDeliveryComplete == 0) {
                                flagDeliveryComplete++;
                                v.startAnimation(playAnimation());

                                handler.postDelayed(new Runnable() {

                                    @Override
                                    public void run() {
                                        // TODO Auto-generated method stub
                                        v.clearAnimation();

                                        OrderitemdetailsBO orderitemdetailsBO = list.get(position); 
                                        Date date = orderitemdetailsBO.getPreparationstarttime();
                                        String preparationComplete = orderitemdetailsBO.getOrderpreparationtime();

                                        if(date != null && preparationComplete != null )
                                        {
                                            Date currentTime = new Date();
                                            Calendar calendar = Calendar.getInstance();

                                            viewHolder.txtElapsedTimeForDeliveryComplete.stop();
                                            orderitemdetailsBO.setOrderdeliverytime(calendar.get(Calendar.MINUTE) +":"+calendar.get(Calendar.SECOND));

                                            String orderDetails = Integer.toString(order_delivered);
                                            String getPosition = Integer.toString(position);
                                            viewHolder.btnDeliveryComplete.setBackgroundColor(Color.LTGRAY);
                                            new sendOrderStatusToServer().execute(orderDetails,getPosition);
                                        }
                                        else
                                        {
                                            Toast.makeText(myContext, "Please Enter Preparation Start Time & Preparation Complete Time.",
                                                    Toast.LENGTH_LONG).show();
                                        }
                                    }
                                }, 5000);

                            }

                            else {

                                handler.removeCallbacksAndMessages(null);
                                v.clearAnimation();
                                flagDeliveryComplete = 0;
                            }
                        }
                    });
            }           

        return convertView;
    }
}

private static class ViewHolder {

    protected TextView txtTableNumber;
    protected TextView txtMenuItem;
    protected TextView txtQuantity;
    protected TextView txtOrderAcceptanceTime;
    protected Chronometer txtElapsedTimeOfOrderAcceptance;
    protected Button btnPreparationStart;
    protected Chronometer txtElapsedTimeForPreparation;
    protected Button btnPreparationComplete;
    protected Chronometer txtElapsedTimeForDeliveryComplete;
    protected Button btnDeliveryComplete;
}

© Stack Overflow or respective owner

Related posts about android

Related posts about listview