0

正如标题所说,我有一个类似的列表视图

|默认图片 | 正文| 复选框 |


我可以在滚动列表视图时保存复选框状态并保持其状态,但我无法修复与图像视图相同的行为

这是我的 getvew()

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

ViewHolder viewHolder = null;
if (convertView == null) {
    LayoutInflater inflator = context.getLayoutInflater();
    convertView = inflator.inflate(R.layout.row, null);
    viewHolder = new ViewHolder();
    viewHolder.text = (TextView) convertView.findViewById(R.id.label);
    viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
    viewHolder.imageview= (ImageView) convertView.findViewById(R.id.imageView1);
    viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                    list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                }
            });
    convertView.setTag(viewHolder);
    convertView.setTag(R.id.label, viewHolder.text);
    convertView.setTag(R.id.check, viewHolder.checkbox);
    convertView.setTag(R.id.imageView1, viewHolder.imageview);

} else {
    viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkbox.setTag(position); // This line is important.
viewHolder.imageview.setTag(position);
viewHolder.text.setText(list.get(position).getName());
viewHolder.checkbox.setChecked(list.get(position).isSelected());


//        change the default-image here
if(list.get(position).getcontact_id()==5)
{
    viewHolder.imageview.setImageResource(android.R.drawable.ic_dialog_map);
}

return convertView;

}

4

3 回答 3

1

问题是,您仅在 getcontact_id() == 5 时更新图像

if(list.get(position).getcontact_id() == 5) {
    viewHolder.imageview.setImageResource(android.R.drawable.ic_dialog_map);
} else {
    viewHolder.imageview.setImageResource(/* set some image here */);
}
于 2013-09-02T09:33:06.497 回答
1

我也遇到过类似的问题..使用convertview删除检查是否为空..

if (convertView == null) {}

尝试膨胀视图并设置它..我已经解决了我的问题..尝试它可能会工作..

于 2013-09-02T09:35:24.993 回答
0

试试这个方法

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

        ViewHolder viewHolder = null;
         if ((convertView == null) || (convertView.getTag() == null)) {
            LayoutInflater inflator = context.getLayoutInflater();
            convertView = inflator.inflate(R.layout.row, null);
            viewHolder = new ViewHolder();


        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.text = (TextView) convertView.findViewById(R.id.label);
        viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
        viewHolder.imageview = (ImageView) convertView.findViewById(R.id.imageView1);
        viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int getPosition = (Integer) buttonView.getTag(); // Here we
                                                                    // get
                                                                    // the
                                                                    // position
                                                                    // that
                                                                    // we
                                                                    // have
                                                                    // set
                                                                    // for
                                                                    // the
                                                                    // checkbox
                                                                    // using
                                                                    // setTag.
                list.get(getPosition).setSelected(buttonView.isChecked()); // Set
                                                                            // the
                                                                            // value
                                                                            // of
                                                                            // checkbox
                                                                            // to
                                                                            // maintain
                                                                            // its
                                                                            // state.
            }
        });


        viewHolder.checkbox.setTag(position); // This line is important.
        viewHolder.imageview.setTag(position);
        viewHolder.text.setText(list.get(position).getName());
        viewHolder.checkbox.setChecked(list.get(position).isSelected());

        // change the default-image here
        if (list.get(position).getcontact_id() == 5) {
            viewHolder.imageview.setImageResource(android.R.drawable.ic_dialog_map);
        }
        convertView.setTag(viewHolder);
        return convertView;
    }
于 2013-09-02T09:40:43.380 回答