【问题标题】:Accessing ListView CheckBox in Android在 Android 中访问 ListView CheckBox
【发布时间】:2017-02-03 09:55:42
【问题描述】:

我正在 Android 中制作待办事项列表应用程序。除了我无法以编程方式设置的 ListView 中有复选框外,一切都运行良好。我正在使用一个 SQLite 数据库来存储我的任务以及它们是否被标记为完成。当我选中一个复选框时,它成功地在我的数据库中保存了一个“1”。我有一个 updateUI 方法,它从我的数据库中读取我的任务文本并显示它。我创建了一个新的“updateUICheckBox”方法来处理复选框的 UI 更新。现在我只是想把我所有的复选框都设置为选中状态,但是当我调用 setChecked(true) 时我得到了一个空指针异常。

// not working
private void updateUICheckBox() {
    CheckBox c = (CheckBox) findViewById(R.id.list_complete_check_box);
    c.setChecked(true); // null pointer exception
}

【问题讨论】:

标签: java android android-layout listview checkbox


【解决方案1】:

由于您设置 CheckBox 是 ListView 项,因此您必须在 ListView 本身的适配器中设置它。并操作数据模型以选中或取消选中 CheckBox。

通过扩展 BaseAdapter 来创建新类以填充 ListView。如果您的自定义布局需要管理超过 1 个 UI 对象,请不要使用 ArrayAdapter

【讨论】:

  • 我添加了一个扩展 BaseAdapter 的内部类(参见上面的编辑)。我填写了getView方法。如何在我的 updateUICheckBox 方法中利用它来根据数据库值设置复选框?
【解决方案2】:

尝试在您的列表中添加一个标志

public class Country {
    public String name;
    public String code;
    public String abbreviation;
    public boolean selected = false;   //--> example this flag
    public int image;
}

适配器

public class CountryCodeAdapter extends BaseAdapter {
    private Context context;
    private List<Country> countryList;


    public CountryCodeAdapter(Context context, List<Country> countryList) {
        this.context = context;
        this.countryList = countryList;
    }

    @Override
    public int getCount() {
        return countryList.size();
    }

    @Override
    public Object getItem(int position) {
        return countryList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_country_code, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Country country = countryList.get(position);
        viewHolder.tv_item_country_name.setText(country.getName());

        String plus = context.getResources().getString(R.string.plus);
        viewHolder.tv_item_country_code.setText(plus.concat(country.getCode()));
        int image = country.getImage();
        if (image != -1) {
            viewHolder.iv_item_country.setImageResource(image);
        }

        return convertView;
    }

    public void updateList(List<Country> countryList) {
        this.countryList = countryList;
        notifyDataSetChanged();
    }

    static class ViewHolder {
        @Bind(R.id.iv_item_country)
        ImageView iv_item_country;
        @Bind(R.id.tv_item_country_name)
        TextView tv_item_country_name;
        @Bind(R.id.tv_item_country_code)
        TextView tv_item_country_code;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}

如果要修改标志,请在适配器外部进行

CountryCodeAdapter adapter = new CountryCodeAdapter(activity, countryList);
//eg. you may change the 1st element in countryList selected flag into true and //call updateList 
        if (adapter != null) {
            adapter.updateList(countryList);
        }

使用此标志在适配器内设置您的复选框。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    相关资源
    最近更新 更多