且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

一个ListView行内观活动

更新时间:2022-05-13 01:08:21

我认为这不是一个好soloution每个视图与值更新时间assing新OnCheckedChangeListener。这将是***只有一个时间设置一个监听器哪里在这个新的观点得到inflatet角度的视图。您可以通过将它变成CB的标签(setTag(..)),使其可用于监听邻对象。需要注意的是onCheckedChanged事件还激发当您通过cb.setChecked()设置CB的状态。

I think it's not a good soloution to assing a new OnCheckedChangeListener each time a view is updated with values. It would be better setting a listener only one time to a view at the point where this new view gets inflatet. You can make o-Object available for the listener by putting it into cb's tag (setTag(..)). Note that the onCheckedChanged event also fires when you set CB's state by cb.setChecked().

例如:

CheckBox cb = null;
if (v == null) {
    LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);
    v = vi.inflate(R.layout.row, null);
    cb = (CheckBox) v.findViewById(R.id.caught);
    // set a listener
    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            item o = (item) buttonView.getTag();
            // show your dialog
        }
    });
}

item o = items.get(position);
if (o != null) {
    TextView tt = (TextView) v.findViewById(R.id.name);
    TextView bt = (TextView) v.findViewById(R.id.game);
    cb = (CheckBox) v.findViewById(R.id.caught);
    cb.setTag(o);
    // set checked state & text
    // ...
}