Android的清爽与复选框的onClick的TextView? [英] Android refreshing TextView with checkbox onClick?

查看:198
本文介绍了Android的清爽与复选框的onClick的TextView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图动态更新(改变颜色和删除线的文本),一个ListView中一个TextView,当用户检查一个复选框。

I'm trying to dynamically update (change color and strikethrough text) a TextView within a ListView when the user checks a CheckBox.

我的code工作有点,但由于某种原因,只在复选框即用户检查什么也没有发生,用户未检查的第二次点击,并根据需要检查第二次的TextView然后更新?

My code works somewhat but for some reason only on the second click of the checkbox i.e. user checks nothing happens, user un-checks and checks a second time TextView then updates as required?

任何帮助AP preciated。

Any help appreciated.

我的code:

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

    View view = convertView;
    if (view == null) {
        view = lInflater.inflate(R.layout.shoppinglist, parent, false);
    }

    String anItem = get_Item(position);//from item array

    ((TextView) view.findViewById(R.id.tvSLItemName)).setText(anItem);

    final CheckBox checkBox = (CheckBox) view.findViewById(R.id.cbBoxSL);

    checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            View aView = convertView;
            if (aView == null) {
                aView = lInflater.inflate(R.layout.shoppinglist, parent, false);
            }
            if(isChecked){  

                checked[position] = true;
                ((TextView) aView.findViewById(R.id.tvSLItemName)).setTextColor(Color.GRAY);
                ((TextView) aView.findViewById(R.id.tvSLItemName)).setPaintFlags(
                        ((TextView) aView.findViewById(R.id.tvSLItemName)).getPaintFlags()
                        | Paint.STRIKE_THRU_TEXT_FLAG);
                //aView.invalidate();
                notifyDataSetChanged();

            }
            else{
                checked[position] = false;
            }

        }
    });

    checkBox.setChecked(checked[position]);
    return view;
}

更新code如下建议:

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

    View view = convertView;
    if (view == null) {
        view = lInflater.inflate(R.layout.shoppinglist, parent, false);
    }

    String anItem = get_Item(position);//from item array

    ((TextView) view.findViewById(R.id.tvSLItemName)).setText(anItem);

    final CheckBox checkBox = (CheckBox) view.findViewById(R.id.cbBoxSL);

    checkBox.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            View aView = convertView;
            if (aView == null) {
                aView = lInflater.inflate(R.layout.shoppinglist, parent, false);
            }
            if(((CheckBox) v).isChecked()) {
                checked[position] = true;
                ((TextView) aView.findViewById(R.id.tvSLItemName)).setTextColor(Color.GRAY);
                ((TextView) aView.findViewById(R.id.tvSLItemName)).setPaintFlags(
                        ((TextView) aView.findViewById(R.id.tvSLItemName)).getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                //aView.invalidate();
                notifyDataSetChanged();
            }else{
                checked[position] = false;
            }

        }
    });

    checkBox.setChecked(checked[position]);
    return view;
}

没有改变问题的陈述。

推荐答案

好吧,我解决了它,搞不清发生了什么事情,也许有人能详细解释?总之,问题是我一再声明的TextView的getView()内,简单地宣布它为变量(TV在这种情况下),那么该方法中调用电视工作。感谢您的帮助。

Well I solved it, not quite sure what was was going on, maybe someone can explain in detail? Anyhow, the issue was with me declaring the TextView repeatedly within the getView(), simply declaring it as a variable (tv in this case) then calling tv within the method worked. Thanks for the help.

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

        View view = convertView;

        if (view == null) {
            view = lInflater.inflate(R.layout.shoppinglist, parent, false);
        }
        final TextView tv = (TextView)view.findViewById(R.id.tvSLItemName);
        String anItem = get_Item(position);//from item array
        tv.setText(anItem);
        final CheckBox checkBox = (CheckBox) view.findViewById(R.id.cbBoxSL);        
        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                View aView = convertView;
                if (aView == null) {
                    aView = lInflater.inflate(R.layout.shoppinglist, parent, false);
                }
                if(isChecked){                          
                    checked[position] = true;                       
                    tv.setTextColor(Color.GRAY);                    
                    tv.setPaintFlags(
                            ((TextView) aView.findViewById(R.id.tvSLItemName)).getPaintFlags()
                            | Paint.STRIKE_THRU_TEXT_FLAG);
                    notifyDataSetChanged();     
                }
                else{
                    checked[position] = false;
                }                   
            }
        });         
        checkBox.setChecked(checked[position]);
        return view;
    }

这篇关于Android的清爽与复选框的onClick的TextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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