0

我在网上的教程中找到了这段代码,如果有点奇怪,我深表歉意,但这是我发现的唯一有效的代码。

我有一个ListView,其中每一行都有一个CheckBox和一个TextView。单击页面上的按钮时,带有选中复选框的行的名称将显示TextViewListView.

只要检查了至少一行,这一切都可以正常工作;如果我选中 4 个复选框并按下按钮,则显示所有 4 个名称,然后取消选中 2,只显示剩余的 2 个名称。

但是,我的问题是,当没有选中任何复选框时,TextView会显示最后一个CheckBox未选中的名称(假设自我启动应用程序以来至少已选中一个)。

为什么会这样,当没有选中复选框时,如何不显示任何内容?

这是在 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);

     }

   }
   }
  });

编译或运行时没有出现错误,所以很遗憾我不能发布 logCat!

谢谢您的帮助

4

3 回答 3

0
@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);
        }
    }
}

考虑没有checkboxes选中任何一个的情况。按逻辑,if (country.isSelected())将返回false。所以,里面的代码if-block甚至不会被执行一次。并且 TextView 的值将保持与以前相同。

解决方案:将查找和设置文本移动到TextView,直到for循环结束。

现在,如果没有任何if语句评估为true(即没有一个checkboxes被检查),变量stringName将保存 的值"" (empty String),这正是它应该的值。

于 2013-08-31T16:46:59.527 回答
0

为了避免 ListView 中 CheckBoxes 的问题,您可以在开始时采用一个初始化为 false 的布尔数组,然后在 ListView 中选中复选框的数组中的相应位置设为 true。当您在应用程序中前进和后退时,这不会导致复选框的选中状态出现问题。

于 2013-08-31T04:05:22.767 回答
0

这里 checkboxstate 是布尔数组:

holder.checkbox.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    if (((CheckBox) v).isChecked())
                        checkBoxState[pos] = true;
                    else
                        checkBoxState[pos] = false;

                }
            });
于 2013-08-31T09:27:19.073 回答