如何禁用listview中的按钮

时间:2016-01-27 08:09:22

标签: android listview

我是Android开发的新手,我很难在脑袋listviews周围徘徊。我几乎经历了整个谷歌搜索,并尝试了每一个"可能"解决方案,但没有真正得到listview,我很难解决这个问题。

当我点击listview项目中的complete_btn时,我想禁用特定按钮。

现在,complete_btn.setOnClickListener部分中的else {}给了我空指针异常(if(converView == null)部分很好)。如果我删除代码,一切正常,但即使只是在侦听器中注释掉所有内容也不起作用。

如果在按下按钮时弹出的“警告”对话框中单击“是”,我想最终禁用该按钮!有人可以帮帮我吗?

public View getView(final int position, View convertView, final ViewGroup parent) {

    View itemView;
    if (convertView == null) {
        itemView = layoutInflater.inflate(R.layout.activity_selected_delivery_item, parent, false);

            final Deliveryltem deliveryltemPosition = epicerieDelivery.selectedDeliveryItem.get(position);

            icon = (ImageView) itemView.findViewById(R.id.selected_delivery_img);
            name = (TextView) itemView.findViewById(R.id.selected_item_name);
            phone_tx = (TextView) itemView.findViewById(R.id.selected_item_phone);
            complete_btn = (Button) itemView.findViewById(R.id.selected_complete_btn);

            if(deliveryltemPosition.order_taken_str.equals("2")){
                complete_btn.setEnabled(false);
            }
            phone_tx.setText(deliveryltemPosition.recipient_phonenum);

        complete_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((ListView) parent).performItemClick(v, position , 0);
                try{
                    order_seq = deliveryltemPosition.order_seq;
                    position_sms = position;
                    Deliveryltem deliveryltemPosition1 = epicerieDelivery.selectedDeliveryItem.get(position);
                    String name = deliveryltemPosition1.recipient_name;
                    String phone = deliveryltemPosition1.recipient_phonenum;
                    AlertDialog dialog = createdialogBox_finish(name, phone, position);
                    dialog.show();
                }catch (Exception e){

                }
            }
        });
        return itemView;
    }else{
        itemView = convertView;
        if(epicerieDelivery.selectedDeliveryItem.size() != 0){
            final Deliveryltem deliveryltemPosition = epicerieDelivery.selectedDeliveryItem.get(position);


            name = (TextView) itemView.findViewById(R.id.selected_item_name);
            phone_tx = (TextView) itemView.findViewById(R.id.selected_item_phone);
            complete_btn = (Button) itemView.findViewById(R.id.selected_complete_btn);

            if(deliveryltemPosition.order_taken_str.equals("2")){
                complete_btn.setEnabled(false);
            }


            complete_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((ListView) parent).performItemClick(v, position , 0);
                    try{
                        order_seq = deliveryltemPosition.order_seq;
                        position_sms = position;
                        Deliveryltem deliveryltemPosition1 = epicerieDelivery.selectedDeliveryItem.get(position);
                        String name = deliveryltemPosition1.recipient_name;
                        String phone = deliveryltemPosition1.recipient_phonenum;
                        buttons.add(complete_btn);
                        AlertDialog dialog = createdialogBox_finish(name, phone, position);
                        dialog.show();
                    }catch (Exception e){

                    }
                }
            });
        }
        return convertView;
    }}


 private AlertDialog createdialogBox_finish(String name, String phone, int position1){

    final String name_str = name;
    final String phone_str = phone;
    courier_id = selectedActivity2.courier_id;
    int button_pos = position1;
    final String message_finish = "message_content";
    buttons.get(0).setEnabled(false);
    buttons.clear();

    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setMessage("배송을 완료하셨습니까?");
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            try{
                SmsManager smsManager = SmsManager.getDefault();
                if (message_finish.length() > 60) {
                    ArrayList<String> contents = smsManager.divideMessage(message_finish);
                    for(int j = 0; j<contents.size(); j++){
                        smsManager.sendTextMessage(phone_str, null, contents.get(j), null, null);
                    }
                } else {                   
                   smsManager.sendTextMessage(phone_str, null, message_finish, null, null);
                }

            }catch (Exception e){
            }
        }
    });

    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    AlertDialog dialog = builder.create();
    return dialog;
}

1 个答案:

答案 0 :(得分:0)

添加

Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
button.setEnabled(false);

里面

builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
          }

方法如:

builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
            button.setEnabled(false);

            try{
                SmsManager smsManager = SmsManager.getDefault();
                if (message_finish.length() > 60) {
                    ArrayList<String> contents = smsManager.divideMessage(message_finish);
                    for(int j = 0; j<contents.size(); j++){
                        smsManager.sendTextMessage(phone_str, null, contents.get(j), null, null);
                    }
                } else {                   
                   smsManager.sendTextMessage(phone_str, null, message_finish, null, null);
                }

            }catch (Exception e){
            }
        }
    });