需要查看列表视图中的复选框是否已选中

时间:2011-10-04 11:57:33

标签: android

我使用layoutinflator创建了listview。 Listview包含每行的复选框和textview。我在列表视图中显示项目,默认选中复选框。我可以检查\取消选中这些项目。

现在在同一个布局上我有一个按钮,在这个按钮的下方,我想检查哪个项目的复选框处于选中状态,哪个没有。

任何人都知道如何检查这个吗?

我的充气代码是:

public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;

    try {
        mydbadapter.open();
        if (view == null) {
            LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.syncissue_row, null);
        }
        issue = (TextView) view.findViewById(R.id.issuedescription);
        checkbox = (CheckBox) view.findViewById(R.id.issueCheckBox);
        issue.setText(arrlstFaults.get(position));
        checkbox.setChecked(true);
    } catch (SQLException e) {
        mydbadapter.close();
        e.printStackTrace();
    } finally {
        mydbadapter.close();
    }
    return view;
}

1 个答案:

答案 0 :(得分:1)

我的答案不是标准答案,但如果您没有找到任何其他选择,您可以选择。 您可以更新getView()方法,如下所示。

public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;

    try {
        mydbadapter.open();
        if (view == null) {
            LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.syncissue_row, null);
        }
        issue = (TextView) view.findViewById(R.id.issuedescription);
        checkbox = (CheckBox) view.findViewById(R.id.issueCheckBox);
        issue.setText(arrlstFaults.get(position));
        // add bellow line
        checkbox.setOnCheckedChangeListener(new MyCheckedChangeListener(position));
        checkbox.setChecked(true);
    } catch (SQLException e) {
        mydbadapter.close();
        e.printStackTrace();
    } finally {
        mydbadapter.close();
    }
    return view;
}

你可以写检查事件改变的监听器。比如波纹管。您可以维护一个Hashtable来存储检查状态,如下所示。

 Hashtable ht = new Hashtable<Integer, Boolean>();

class MyCheckedChangeListener implements OnCheckedChangeListener
{
    int position = -1;
    myCheckedChangeListener(int position){
        this.position = position;
    }

    @Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

   ht.put(position, isChecked);
}

}

我希望它对你有用。

相关问题