如何在滚动时保持android自定义列表视图中的按钮状态

时间:2012-12-14 05:02:21

标签: android android-listview android-button

我的自定义ListView有三个TextView三个Button和三个Chronometer。情况是我正在正确加载ListView。但是在加载ListView时,我通过检查一个参数来禁用ListView中的某个按钮。到目前为止,ListView正确地显示了它的行。

但是当我滚动ListView时,之前启用的Button正在被禁用。我做错了什么我没有得到可以请指出我的错误或任何建议。

这是我的Adapter类。

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

0 个答案:

没有答案