Only 1 button enable in ListView, custom adapter

微笑、不失礼 提交于 2019-12-25 04:11:48

问题


I run into a problem where I need to enable my button in the ListView. The weird thing is :

 public class CookingStepAdapter extends ArrayAdapter<CookingStep> {

...
       private void addButtonToList(Button clock, Button skip){
            if (list_clock_button == null) {
                list_clock_button = new ArrayList<Button>();
                iterate = 0;
            }
            if (list_skip_button == null)
                list_skip_button = new ArrayList<Button>();
            list_clock_button.add(clock);
            list_skip_button.add(skip);

            clock.setEnabled(true);
            skip.setEnabled(true);

            list_clock_button.get(0).setEnabled(true);
            list_clock_button.get(0).setFocusable(true);
            list_clock_button.get(0).setFocusableInTouchMode(true);
            list_clock_button.get(0).invalidate();

            list_skip_button.get(0).setEnabled(true);
            list_skip_button.get(0).setFocusable(true);
            list_skip_button.get(0).setFocusableInTouchMode(true);
            list_skip_button.get(0).postInvalidate();
        }
}

When I set enable with the list_clock_button.get(0), it's not working at all. But clock.setEnabled(true); actually worked.

But then I only want the first button of the ListView enabled, that makes the first option more fit in this situation. The second option works, but it made all the buttons enabled, that's not what I want. I did recheck the first button address, and it matched list_clock_button.get(0), why it's not working.

EDIT :

Here's my function getView :

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.cooking_steps_and_timer, parent, false);

        final Button button = (Button) rowView.findViewById(R.id.button_timer);
        final TextView timer = (TextView) rowView.findViewById(R.id.cooking_timer);
        final Button skipButton = (Button) rowView.findViewById(R.id.button_skip);
        TextView stepContent = (TextView) rowView.findViewById(R.id.cooking_step_content);

        final CookingStep step = list.get(position);
        String stepOrder = (String) context.getResources().getText(R.string.step_order) + " " + step.getOrder();
        String content = "<b>" + stepOrder + ":</b>" + " " + step.getContent() + "\n";
        stepContent.setText(Html.fromHtml(content));

        if (step.getTimer() == null || step.getTimer() == 0){
            timer.setVisibility(View.GONE);
            button.setVisibility(View.GONE);
            skipButton.setVisibility(View.GONE);
        } else {

            //myTimer = new CookingTimer(step.getTimer());
            timer.setText(step.getMyTimer().toString());
            step.setCountDown(new CountDownTimer(step.getTimer() * 60000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    step.getMyTimer().tick();
                    timer.setText(step.getMyTimer().toString());
                }

                @Override
                public void onFinish() {
                    nextButtonEnable();
                }
            });

            button.setText(R.string.button_available);
            skipButton.setText(R.string.skip_button_content);

            addButtonToList(button, skipButton);
            //button.setEnabled(true);

            list_clock_button.get(0).setEnabled(true);

            button.requestFocusFromTouch();
            button.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {

                        if (!button.isPressed()) {
                            button.setPressed(true);
                            button.setText(R.string.button_available);
                            step.getCountDown().start();
                        } else {
                            button.setPressed(false);
                            button.setText(R.string.button_pressed);
                            step.getCountDown().cancel();

                        }
                    }
                    return true;
                }
            });

            skipButton.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        step.getCountDown().cancel();
                        nextButtonEnable();
                    }
                    return true;
                }
            });
        }
        return rowView;
    }

回答1:


The reason of all buttons getting enabled is you are setting all of them enable yourself. To set any particular button enable you have to check the id or the item position to set that particular button to enable. Like this for example:-

if(clock.getID == R.id.some_id_in_xml){
    clock.setEnabled(true);
}

But before that you have to check the position of the item to select the desired item in the list.



来源:https://stackoverflow.com/questions/32390626/only-1-button-enable-in-listview-custom-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!