只有1按钮的ListView启用自定义适配器 [英] Only 1 button enable in ListView, custom adapter

查看:216
本文介绍了只有1按钮的ListView启用自定义适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰上,我需要能够在ListView我的按钮有问题。奇怪的是:

 公共类CookingStepAdapter扩展ArrayAdapter< CookingStep> {...
       私人无效addButtonToList(钮钟,按钮跳过){
            如果(list_clock_button == NULL){
                list_clock_button =新的ArrayList<按钮和GT;();
                迭代= 0;
            }
            如果(list_skip_button == NULL)
                list_skip_button =新的ArrayList<按钮和GT;();
            list_clock_button.add(时钟);
            list_skip_button.add(跳过);            clock.setEnabled(真);
            skip.setEnabled(真);            list_clock_button.get(0).setEnabled(真);
            list_clock_button.get(0).setFocusable(真);
            list_clock_button.get(0).setFocusableInTouchMode(真);
            list_clock_button.get(0).invalidate();            list_skip_button.get(0).setEnabled(真);
            list_skip_button.get(0).setFocusable(真);
            list_skip_button.get(0).setFocusableInTouchMode(真);
            list_skip_button.get(0).postInvalidate();
        }
}

当我设定启用 list_clock_button.get(0),它不工作的。但 clock.setEnabled(真); 实际工作。

但后来我只想要启用的ListView,使第一种方案更适合在这种情况下的第一个按钮。第二个选项的作品,但它使使所有的按钮,这不是我想要的。我做了重新检查的第一个按钮的地址,它匹配 list_clock_button.get(0),为什么它不工作。

编辑:

下面是我的功能getView:

  @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        查看rowView = inflater.inflate(R.layout.cooking_steps_and_timer,父母,假);        最后Button按钮=(按钮)rowView.findViewById(R.id.button_timer);
        最终的TextView计时器=(TextView中)rowView.findViewById(R.id.cooking_timer);
        最终按钮skipButton =(按钮)rowView.findViewById(R.id.button_skip);
        TextView的stepContent =(TextView中)rowView.findViewById(R.id.cooking_step_content);        最后一步CookingStep = list.get(位置);
        。字符串stepOrder =(字符串)context.getResources()的getText(R.string.step_order)++ step.getOrder();
        字符串内容=< B>中+ stepOrder +:其中; / B>中++ step.getContent()+\\ n;
        stepContent.setText(Html.fromHtml(内容));        如果(step.getTimer()== NULL || step.getTimer()== 0){
            timer.setVisibility(View.GONE);
            button.setVisibility(View.GONE);
            skipButton.setVisibility(View.GONE);
        }其他{            // myTimer =新CookingTimer(step.getTimer());
            timer.setText(step.getMyTimer()的toString());
            step.setCountDown(新CountDownTimer(step.getTimer()* 60000,1000){
                @覆盖
                公共无效onTick(长millisUntilFinished){
                    step.getMyTimer()打勾()。
                    timer.setText(step.getMyTimer()的toString());
                }                @覆盖
                公共无效onFinish(){
                    nextButtonEnable();
                }
            });            button.setText(R.string.button_available);
            skipButton.setText(R.string.skip_button_content);            addButtonToList(按钮,skipButton);
            //button.setEnabled(true);            list_clock_button.get(0).setEnabled(真);            button.requestFocusFromTouch();
            button.setOnTouchListener(新View.OnTouchListener(){
                @覆盖
                公共布尔onTouch(视图V,MotionEvent事件){
                    如果(event.getAction()== MotionEvent.ACTION_UP){                        如果(!button.is pressed()){
                            button.set pressed(真);
                            button.setText(R.string.button_available);
                            。step.getCountDown()开始();
                        }其他{
                            button.set pressed(假);
                            button.setText(R.string.button_ pressed);
                            step.getCountDown()取消()。                        }
                    }
                    返回true;
                }
            });            skipButton.setOnTouchListener(新View.OnTouchListener(){
                @覆盖
                公共布尔onTouch(视图V,MotionEvent事件){
                    如果(event.getAction()== MotionEvent.ACTION_UP){
                        step.getCountDown()取消()。
                        nextButtonEnable();
                    }
                    返回true;
                }
            });
        }
        返回rowView;
    }


解决方案

启用得到所有按钮的原因是要设置所有这些使自己。要设定任何特定的按钮,让你不得不检查ID或项目位置来设置特定的按钮启用。
这样,例如: -

 如果(clock.getID == R.id.some_id_in_xml){
    clock.setEnabled(真);
}

但在此之前,你必须检查的项目的位置,在列表中选择所需的项目。

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

解决方案

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.

这篇关于只有1按钮的ListView启用自定义适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆