| default-Image |文|复选框|
我可以在滚动列表视图的同时保存复选框状态并保持其状态,但我无法使用imageview修复相同的bevahiour
这是我的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;
}
答案 0 :(得分: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 */);
}
答案 1 :(得分:1)
我也遇到了同样的问题。使用convertview进行的检查是否为null ..
if (convertView == null) {}
尝试给视图充气并设置它..我已经解决了我的问题......它可能会起作用..
答案 2 :(得分: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;
}