无法设置OnCheckedChangeListener一个复选框 [英] Can't set OnCheckedChangeListener to a Checkbox

查看:162
本文介绍了无法设置OnCheckedChangeListener一个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设定一个 OnCheckedChangeListener 复选框,但我的应用程序在运行时退出。我也试着设置监听我的的TextView ,我仍然得到同样的结果。任何人都可以帮忙吗?

 进口android.app.ListActivity;
进口android.content.Context;
进口android.content.DialogInterface;
进口android.content.DialogInterface.OnClickListener;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ArrayAdapter;
进口android.widget.CheckBox;
进口android.widget.CompoundButton;
进口android.widget.CompoundButton.OnCheckedChangeListener;
进口android.widget.ImageView;
进口android.widget.ListView;
进口android.widget.TextView;
进口android.widget.Toast;

公共类ListViewActivity扩展ListActivity实现OnCheckedChangeListener {

TextView的标签;
复选框复选框;

公共类MyCustomAdapter扩展ArrayAdapter<字符串> {


公共MyCustomAdapter(上下文的背景下,INT textViewResourceId,
的String []对象){
超(背景下,textViewResourceId,对象);

 // TODO自动生成构造函数存根
}

@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
// TODO自动生成方法存根
//返回super.getView(位置,convertView,父母);

查看排= convertView;

如果(行== NULL){
LayoutInflater充气= getLayoutInflater();
行= inflater.inflate(R.layout.main,父母,假);

}


标签=(TextView中)row.findViewById(R.id.weekofday);
label.setText(月[位置]);
复选框=(复选框)row.findViewById(R.id.checkBox);


返回行;
}
}

的String []月= {
月,月,月,月,
五一,六一,七一,八一,
九·一,十月,月,月
};

 / **第一次创建活动时调用。 * /
 @覆盖
 公共无效的onCreate(包savedInstanceState){
 super.onCreate(savedInstanceState);
//的setContentView(R.layout.main);
 / * setListAdapter(新ArrayAdapter<字符串>(这一点,
   R.layout.row,R.id.weekofday,星期几)); * /


   setListAdapter(新MyCustomAdapter(ListViewActivity.this,R.layout.main,一个月));

   checkBox.setOnCheckedChangeListener(本); //我的应用程序退出这里!!!!

 }





@覆盖
公共无效onCheckedChanged(CompoundButton为arg0,布尔ARG1){
// TODO自动生成方法存根
//Toast.makeText(getApplicationContext()中选中,Toast.LENGTH_LONG);

}


}
 

解决方案

您不能设置监听你的复选框的ListView 这样的(它可能会抛出一个 NullPointerException异常),而不是设置在 getView()办法(你也必须保持复选框状态,这样你就不会结束与陌生行状态)。贝娄是一个例子:

 公共类ListViewActivity扩展ListActivity {

    公共类MyCustomAdapter扩展ArrayAdapter<字符串> {

        私人的ArrayList<布尔>状态=新的ArrayList<布尔>();

        公共MyCustomAdapter(上下文的背景下,INT textViewResourceId,
                的String []对象){
            超(背景下,textViewResourceId,对象);
            的for(int i = 0; I< objects.length;我++){
                status.add(假);
            }
        }

        @覆盖
        公共查看getView(最终诠释的立场,观点convertView,
                ViewGroup中父){
            查看排= convertView;
            如果(行== NULL){
                LayoutInflater充气= getLayoutInflater();
                行= inflater.inflate(R.layout.adapters_listviewactivity_row,
                        父母,假);
            }

            TextView的标签=(TextView中)row.findViewById(R.id.weekofday);
            label.setText(月[位置]);
            复选框复选框=(复选框)row.findViewById(R.id.checkBox);
            checkBox.setOnCheckedChangeListener(新OnCheckedChangeListener(){

                @覆盖
                公共无效onCheckedChanged(CompoundButton buttonView,
                        布尔器isChecked){
                    Toast.makeText(getApplicationContext(),+位置,
                            Toast.LENGTH_SHORT).show();
                    如果(器isChecked){
                        status.set(位置,真正的);
                    } 其他 {
                        status.set(位置,假);
                    }
                }
            });
            checkBox.setChecked(status.get(位置));
            返回行;
        }
    }

    的String []月= {一月,二月,月,月,五一,六一,
            七五,八五,九五,十月,月,十二月};

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        setListAdapter(新MyCustomAdapter(ListViewActivity.this,
                R.layout.main,一个月));
    }

}
 

对于的TextView 你必须做同样的事情。

I am trying to set a OnCheckedChangeListener to a CheckBox but my application exits in the run time. I also tried to set listeners for my TextView and I still get the same result. Can anyone help?

import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListViewActivity extends ListActivity implements OnCheckedChangeListener {

TextView label;
CheckBox checkBox;

public class MyCustomAdapter extends ArrayAdapter<String> {


public MyCustomAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);

 // TODO Auto-generated constructor stub
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return super.getView(position, convertView, parent);

View row = convertView;

if(row==null){
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.main, parent, false); 

}  


label=(TextView)row.findViewById(R.id.weekofday);
label.setText(month[position]);
checkBox=(CheckBox)row.findViewById(R.id.checkBox);


return row;
}
}

String[] month = {
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"
};

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
 /*setListAdapter(new ArrayAdapter<String>(this,
   R.layout.row, R.id.weekofday, DayOfWeek));*/


   setListAdapter(new MyCustomAdapter(ListViewActivity.this, R.layout.main, month));

   checkBox.setOnCheckedChangeListener(this); //my application exits here!!!!

 }





@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "box checked", Toast.LENGTH_LONG);

}


}

解决方案

You can't set the listener for your CheckBox from the ListView like that(it will probably throw a NullPointerException), instead set the listener in the getView() method(you'll also have to keep the CheckBox status so you don't end up with strange rows status). Bellow is an example:

public class ListViewActivity extends ListActivity {

    public class MyCustomAdapter extends ArrayAdapter<String> {

        private ArrayList<Boolean> status = new ArrayList<Boolean>();

        public MyCustomAdapter(Context context, int textViewResourceId,
                String[] objects) {
            super(context, textViewResourceId, objects);
            for (int i = 0; i < objects.length; i++) {
                status.add(false);
            }
        }

        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            View row = convertView;
            if (row == null) {
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.adapters_listviewactivity_row,
                        parent, false);
            }

            TextView label = (TextView) row.findViewById(R.id.weekofday);
            label.setText(month[position]);
            CheckBox checkBox = (CheckBox) row.findViewById(R.id.checkBox);
            checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    Toast.makeText(getApplicationContext(), "" + position,
                            Toast.LENGTH_SHORT).show();
                    if (isChecked) {
                        status.set(position, true);
                    } else {
                        status.set(position, false);
                    }
                }
            });
            checkBox.setChecked(status.get(position));
            return row;
        }
    }

    String[] month = { "January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December" };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new MyCustomAdapter(ListViewActivity.this,
                R.layout.main, month));
    }

}

For the TextView you'll have to do the same thing.

这篇关于无法设置OnCheckedChangeListener一个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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