Java 未注册为未选中的复选框
我在网上的一个教程中发现了这段代码,如果有点奇怪,我深表歉意,但这是我发现的唯一有效的代码 我有一个Java 未注册为未选中的复选框,java,android,listview,android-listview,onclick,Java,Android,Listview,Android Listview,Onclick,我在网上的一个教程中发现了这段代码,如果有点奇怪,我深表歉意,但这是我发现的唯一有效的代码 我有一个列表视图,其中每一行都有一个复选框和一个文本视图。单击页面上的按钮时,带有复选框的行的名称将显示在列表视图上方的单独文本视图中 只要至少选中一行,这一切都可以正常工作;如果我选中4个复选框并按下按钮,将显示所有4个名称,然后取消选中2,仅显示其余2个名称 但是,我的问题是,当没有勾选复选框时,TextView会显示最后一个要取消选中的复选框的名称(假设自启动应用程序以来至少选中了一个复选框) 为什
列表视图
,其中每一行都有一个复选框
和一个文本视图
。单击页面上的按钮时,带有复选框的行的名称将显示在列表视图
上方的单独文本视图
中
只要至少选中一行,这一切都可以正常工作;如果我选中4个复选框并按下按钮,将显示所有4个名称,然后取消选中2,仅显示其余2个名称
但是,我的问题是,当没有勾选复选框时,TextView
会显示最后一个要取消选中的复选框的名称(假设自启动应用程序以来至少选中了一个复选框)
为什么会出现这种情况?如果没有选中复选框,如何使任何内容都不显示
以下是在onCreate中调用的适配器和方法:
private class MyCustomAdapter extends ArrayAdapter<Country> {
private ArrayList<Country> countryList;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<Country> countryList) {
super(context, textViewResourceId, countryList);
this.countryList = new ArrayList<Country>();
this.countryList.addAll(countryList);
}
private class ViewHolder {
TextView item;
CheckBox favourite;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.country_info, null);
holder = new ViewHolder();
holder.item = ((TextView)convertView.findViewById(R.id.infoBox));
holder.favourite = ((CheckBox)convertView.findViewById(R.id.checkBox1));
convertView.setTag(holder);
holder.favourite.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Country country = (Country) cb.getTag();
country.setSelected(cb.isChecked());
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
Country country = countryList.get(position);
holder.item.setText(country.getName());
holder.favourite.setChecked(country.isSelected());
holder.favourite.setTag(country);
return convertView;
}
}
private void checkButtonClick() {
Button myButton = (Button) findViewById(R.id.findSelected);
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String stringName = "";
ArrayList<Country> countryList = dataAdapter.countryList;
for(int i=0;i<countryList.size();i++){
Country country = countryList.get(i);
if(country.isSelected()){
stringName = (stringName + country.getName());
TextView t = ((TextView)findViewById(R.id.textView1));
t.setText(stringName);
}
}
}
});
私有类MyCustomAdapter扩展了ArrayAdapter{
私有ArrayList国家列表;
公共MyCustomAdapter(上下文,int textViewResourceId,
ArrayList国家列表){
super(上下文、textViewResourceId、countryList);
this.countryList=新的ArrayList();
this.countryList.addAll(countryList);
}
私有类视窗持有者{
文本视图项;
复选框收藏夹;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
ViewHolder=null;
Log.v(“ConvertView”,String.valueOf(position));
if(convertView==null){
LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
convertView=vi.充气(R.布局.国家/地区信息,空);
holder=新的ViewHolder();
holder.item=((TextView)convertView.findViewById(R.id.infoBox));
holder.favorite=((复选框)convertView.findViewById(R.id.checkBox1));
convertView.setTag(支架);
holder.favorite.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
复选框cb=(复选框)v;
Country Country=(Country)cb.getTag();
country.setSelected(cb.isChecked());
}
});
}否则{
holder=(ViewHolder)convertView.getTag();
}
Country=countryList.get(位置);
holder.item.setText(country.getName());
holder.favorite.setChecked(country.isSelected());
holder.Favorite.setTag(国家/地区);
返回视图;
}
}
私有无效检查按钮单击(){
按钮myButton=(按钮)findViewById(R.id.findSelected);
myButton.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
字符串stringName=“”;
ArrayList countryList=dataAdapter.countryList;
对于(int i=0;i为了避免ListView中复选框出现问题,您可以在开始时将初始化为false的布尔数组设置为true,然后将数组中复选框在ListView中被选中的对应位置设置为true。当您在应用程序中向前和向后移动时,这不会导致复选框处于选中状态时出现问题。这里checkboxstate是布尔数组:
holder.checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (((CheckBox) v).isChecked())
checkBoxState[pos] = true;
else
checkBoxState[pos] = false;
}
});
@覆盖
公共void onClick(视图v){
字符串stringName=“”;
ArrayList countryList=dataAdapter.countryList;
对于(int i=0;我可以尝试将TextView t=(TextView)findviewbyd(R.id.TextView 1));t.setText(stringName);
移到外部(后面)for loop?我不确定具体是怎么解决的,但这样做已经解决了我的问题,非常感谢!如果你想把它作为一个答案发布,我会接受它。我想这可能会奏效,但是我对android不是很好,我怀疑我是否能正确使用它。另外,我还有一个功能,我只能使用持有者才能使用,所以当前的方法可能会有用使用比布尔数组少得多的额外代码,因为getView等已经被调用。我使用过它,它基本上消除了ListView带有复选框的所有缺点。如果我的答案对你有效,请接受我的答案。我会尝试一下,但事实证明评论者的答案确实有效!谢谢你的帮助,但正如我所说,评论者的答案有效。
@Override
public void onClick(View v) {
String stringName = "";
ArrayList<Country> countryList = dataAdapter.countryList;
for(int i=0;i<countryList.size();i++){
Country country = countryList.get(i);
if(country.isSelected()){
stringName = (stringName + country.getName());
// ---------The problem is here:
// This code is unreachable when none of the checkboxes is checked
TextView t = ((TextView)findViewById(R.id.textView1));
t.setText(stringName);
}
}
}